?
Solved

global variable ....

Posted on 2004-09-22
11
Medium Priority
?
212 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 600 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
Technology Partners: 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 600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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
Course of the Month14 days, 16 hours left to enroll

770 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