Solved

global variable ....

Posted on 2004-09-22
11
208 Views
Last Modified: 2010-03-31
Hi experts,

  I have some java files with the following structure :

A.java :
public class A{
 ...
}

--------------
TestA.java
public class TestA{
   ...
   public static void main(...){ ...
  }
}
-------
  How do I set a global variable which can be read by A.java and TestA.java ??? Thanks !
0
Comment
Question by:meow00
[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
  • 2
  • 2
  • 2
  • +4
11 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12120725
Avoid "Global variables" if possible
Why should you need it?
0
 
LVL 35

Accepted Solution

by:
girionis earned 150 total points
ID: 12120726
You can have an interface and make them implement this interface:

public interface GlobalVariable
{
    public static int global = 10;
}

public class A implement GlobalVariable
{
   System.out.println(global);
}

public class TestA implements GlobalVariable{
   System.out.println(global);
}
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12120732
You can't.

You could make a static 'variable' and create an instance of class A in class TestA

public class A {
   public static int variable;
   ...
}

public class TestA {
   private A myA = new myA();
   ...
   public static void main(...){
     myA.variable = somevalue;
   }
   ...
}

-r-
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:girionis
ID: 12120736
>public class A implement GlobalVariable

should be

public class A implements GlobalVariable
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12120796
Be aware though that this way you don't have a variable you can assign values to...
Just read it
0
 
LVL 92

Expert Comment

by:objects
ID: 12120810
> How do I set a global variable which can be read by A.java and TestA.java ???

Java doesn't have global variables.
Use a class (static) variable as mentioned above.

A.java :
public class A{
   public static int X;

 ...
}

--------------
TestA.java
public class TestA{
   ...
   public static void main(...){

     // to access use
     int x = A.X;

     ...

  }
}
0
 
LVL 3

Expert Comment

by:granbajo
ID: 12120846
Another way of doing it is to define a global variable in one of the classes, and methods to set it and read it, and call those methods from the second class.  Something like this:

A.java :
public class A{

  int globalInt;

  public int getGlobal() {
    return(globalInt);
  }

  public void setGlobal(int i) {
    globalInt = i;
  }
 ...
}

--------------
TestA.java
public class TestA{

A a
   ...
   public static void main(...){ ...
     a.setGlobal(8);
     int x = a.getGlobal();
     
  }
}
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12121455
Everybody who says that JAVA doesnot have global variables has never read the documentation.
(fortunately not everybody is referred by this)
See the constants in Math (PI, E) : who can live without them?
The difference to your own wanted variables is omitting the keyword 'final'
;JOOP!
0
 
LVL 92

Expert Comment

by:objects
ID: 12121477
> See the constants in Math (PI, E) : who can live without them?

constants and global variable are different things :)
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12121513
so therefor static final public classattributes should be called 'globals'?
0
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 150 total points
ID: 12121630
Most of the above comments are correct.  Global variables are NOT part of Object Oriented programming(OOP) and you should avoid them like the plague(like goto's).  Instead you should think of how the data is to be used and create objects that implement the pattern that best fits the usage.  Here are some examlpes.

 -If the value is a constant (i.e. PI, ERROR_MESSAGE_TEMPLATE, ...) create an interface containing the constant values.
     public Interface ApplicationConstants {
        //These probably should be seperated by category
        public final String ERROR_MESSAGE_TEMPLATE="You are a dummy,  why did you do that!?!?!?";
        public final int INCHES_PER_MILE=5280;
        public final double CM_PER_INCH=2.83;  //todo:look up the CORRECT value
    }
 -If the value is a applictation configuration, store them in properties (e.g. defaultWindowSize, dbConnectURL, ...)
 -If the value is part of collection of services, then use the Singleton pattern
      public class Singleton
      {
            private Singleton ref;
            private Singleton()
            {
            }

            public static void getInstance()
            {
                 if (ref==null) {
                      ref=new Singleton();
                 return ref;
            }

            public int getSingletonValue()
            {
                    return this.x;  //This is some computed or queried value that the singleton produces
             }
      }

Don't be afraid of creating lots of classes.  It is better to have many easy to understand class relationships, then one gigantic class that is hard to maintain.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 109
Why my table column Id is not passed to java object? 4 46
ejb wildfly example 2 75
Coding for the first time 9 71
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

697 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