Solved

global variables

Posted on 2006-06-13
13
276 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:mayankeagle
ID: 16895320
Can you post your complete project/ package/ class hierarchy?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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:mayankeagle
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

744 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

10 Experts available now in Live!

Get 1:1 Help Now