Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

variable scope

Posted on 2007-04-01
5
Medium Priority
?
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 120 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 120 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 140 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 120 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

671 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