Solved

variable scope

Posted on 2007-04-01
5
238 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
Comment Utility
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
Comment Utility
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
Comment Utility
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:mayankeagle
mayankeagle earned 30 total points
Comment Utility
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
Comment Utility
:-)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

16 Experts available now in Live!

Get 1:1 Help Now