Solved

variable scope

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

627 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