Solved

variable scope

Posted on 2007-04-01
5
239 Views
Last Modified: 2010-03-31
i have a package with 2 classes (main and puzzle).  i want one variable "size" to be applicable to both classes.  I've tried using the code below. How do I "pass" the variable to the other class?  i could use the constructor but I feel that is wasteful and not necessary. is there another way?  i thought public variables were available to all classes in the package.
 
public class Main {

public static int size;

public Main {
}

public static void main(String[] args) {

size = .....

}

==================

public class puzzle {


for (int i = 0; i < size; i++)...etc.
0
Comment
Question by:jpking72
5 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 30 total points
ID: 18832687
It doesn't have to be public. Since your classes are in the same package you could give access by using *no* modifier
0
 
LVL 10

Assisted Solution

by:ADSLMark
ADSLMark earned 30 total points
ID: 18832947
If it's a size value like in the example then it's good practice to pass this with the constructor like:
//for example
new Puzzle(size);

//constructor
public Puzzle(int size)
{
....
}

Why is this better? Well you can instantiate the puzzle with different sizes. If there is only one size possible then do the following:

class Puzzle
{
public final static int SIZE = 42;
...
}

and you can access this static variable by:
Puzzle.SIZE

For the solution to your problem self, you should say: Main.size since size is a *static* variable in the Main class and thus you need to specify the Main class before the variable.

Mark
0
 
LVL 5

Accepted Solution

by:
tbboyett earned 35 total points
ID: 18833114
Since your variable "size" is in the class main and you don't want to pass it as a parameter in the constructor, all you would need to do is call the variable in you Puzzle class like this:  Main.size

Example:
for (int i = 0; i < Main.size; i++) {

}
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 30 total points
ID: 18833131
If you dont want size to be static, you can create a getter for it:

public int getSize ()
{
  return size ;

}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18835156
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
network + 7 80
advertisement module in core php 4 144
get weblogic logged in user in java 2 57
Need to track down Infection in a Server 2008 domain user profile 7 48
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now