Solved

global variables

Posted on 2006-06-13
13
288 Views
Last Modified: 2010-03-31
Hi:

I wrote some classes under a folder. I also have the other classes (with package declaration) in various packages under the same folder. So the structure is like:
MyFolder
- (default package)
    --MyPublicStaticVariablesClass.java
    --...
- packageA
- packageB

My question are:
1. how can I use a variable in the MyPublicStaticVariableClass in default package from packageA?
2. what is the best way to declare global variables if I prefer to export only package level files when making executables?
 
0
Comment
Question by:sdc248
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16894949
1. simply add this line:

import packageA.*;

2. This question is more trickier, are You using some IDE while developing an app? (like eclipse or NetBeans or any other?)
0
 

Author Comment

by:sdc248
ID: 16895030
1. import packageA from MyPublicStaticVariablesClass? But I am referencing variables in that class FROM packageA, not the other way around.
2. I am using elicpse.
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16895103
As for the first part of Your question:
You should be able to use the class MyPublicStaticVariablesClass in any other classes without importing any packages, as long the java application starting directory path is the one where the MyPublicStaticVariablesClass class is contained. In other words:

class A {
    ... main(...) {}
}
======
package p;
class B {
    ... f(...) {
        A a = new A(...);
    }
}

The class tructure above should work without any additional imports as long as You run the starting application from top directory. Say this way:

java A
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 16895194
Use of the default package is not recommended, particularly when other parts of your tree *are* in packages
0
 

Author Comment

by:sdc248
ID: 16895230
I thought so too. But I got "A cannot be resolved to a type" from eclipse...
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16895320
Can you post your complete project/ package/ class hierarchy?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:sdc248
ID: 16895385
Hi CEHJ:

So the better to make it work is to put those "global" files in another package and then have it imported from the 'client" packages?

Do I simply create a new package,move all the files in it, and add the package declarations? (Want confirmation because they are soruce codes for a couple of running applications.)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16895411
>>So the better to make it work is to put those "global" files in another package and then have it imported from the 'client" packages?

I would do yes. Make, say, a 'constants' package
0
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 16898311
> 1. how can I use a variable in the MyPublicStaticVariableClass in default package from packageA?

you can't

> 2. what is the best way to declare global variables if I prefer to export only package level files when making executables?

They *must* go in packages

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16899856
To make things simpler, can you please reply to: >> "Can you post your complete project/ package/ class hierarchy?"
0
 
LVL 4

Expert Comment

by:astorer
ID: 16901871
>> 1. how can I use a variable in the MyPublicStaticVariableClass in default package from packageA?
import MyPublicStaticVariableClass;

from your class in packageA.
Then can use
    MyPublicStaticVariableClass.globalVar
or better
    MyPublicStaticVariableClass.getGlobalVar()
Remember that globalVar or getGlobalVar() must be public and static

I would say that MyPublicStaticVariableClass should not be in the default package anyway.  Why not just put into a package called statics or util ?

>> 2. what is the best way to declare global variables if I prefer to export only package level files when making executables?
There is no such thing as a global variable in an Object Oriented programming language.
What you do have is static members on a class.  That means that there is only one of these and you access via Class.member (rather than object.member).
These can be used as though they are global variables.

If pushed I'd use:

package statics;
public class MyPublicStaticVariableClass {
  private static String myVar = null;
  public static String getMyVar(){
      return myVar;
  }
  public static void setMyVar(String myVarParam){
      myVar = myVarParam;
  }
}

Then whenever you want to use MyVar
  String x = statics.MyPublicStaticVariableClass.getMyVar()
  statics.MyPublicStaticVariableClass.setMyVar("ABC");

Andrew



 
0
 

Author Comment

by:sdc248
ID: 16911862
Thank you guys for your help. I am putting all shared variables in a class in a seperate package.

p.s.
I tried "import MyPublicStaticVariableClass;" but that didn't work. That class is not visable from packageA.
I did not post my whole project/package/class hierarchy because I think the brief structure provided in the question has provided enough information for problem identification/solving.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16911901
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

920 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

13 Experts available now in Live!

Get 1:1 Help Now