Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

global variables

Posted on 2006-06-13
13
Medium Priority
?
317 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
[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
  • 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
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 86

Accepted Solution

by:
CEHJ earned 500 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 200 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

598 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