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
Solved

global variables

Posted on 2006-06-13
13
301 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 video teaches viewers about errors in exception handling.

808 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