Solved

global variables

Posted on 2006-06-13
13
296 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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
 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

831 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