Solved

variable scope

Posted on 2007-04-01
5
243 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 214
Error in @AspectJ Based AOP with Spring 2 18
printf performancy 11 73
Java program running SQL query 5 38
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

828 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