hi there
i wanted to know if there is something like global or extern variables
(in,c language),which i can use in every class ?!
and my other question is ,if is there a complete tutorial on netbeans
gui builder , because i have lots of problems about jframes.
thanks a lot.

There are no such thing as either golobal nor global vars. There are "constants" i.e. public static final variables, but they still need to be referenced through the class. That can be done in the imports however using a static import. I.E.

// class Test1
package test;

public class Test1 {
  public static final int cons = 1;
}

// Class Test2
package anotherPackage;

import static test.Test1.cons;

public class Test2 {
  public static void main (String[] args) {
    System.out.println(cons);
  }
}
Comments
Thanks for the new info

I didn't know that this was possible. Good.

import static test.Test1.cons;

..

System.out.println(cons);

I usually do it like this:

System.out.println(Test1.cons);

Edited 6 Years Ago by javaAddict: n/a

I didn't know that this was possible. Good.

Yep. Works with all static items, not just final static items, and not just variables.

//Class Test1
package t1;

public class Test1 {
  public static void printIt(String s) {
    System.out.println(s);
  }
}

//Class Test2
package t2;

import static t1.Test1.printIt;

public class Test2 {
  public static void main(String[] args) {
    printIt("Hello");
  }
}

Edit: Note. You can also use "*" instead of the var or method name and get all static items from the class. I would, in general, strenuously object to this. I also do not recommend using static imports at all except for static values that are being used multiple (i.e. more than 2 or 3) times in the "current" class. For the maintenance developer, it quickly becomes confusing. I.E. "Now where does that variable come from?" This effect is much worse with the "*", but can also be, at least, annoying for single values.

Edited 6 Years Ago by masijade: n/a

hi there again!
i lost my pass so i made a new account xlx2.
thanx for answer!
but let me tell you the rest of problem,if i want to make a information
bank with hash table and use it in other classes is the flowing code
right!

import java.util.*;

public class acount_bank {
    private  static Hashtable ht=new Hashtable();

 static boolean  search(information o){
   if(ht.containsKey(o)) return true ;return false;
 }
   static void  add(information o){
      ht.put(o, o);
  }

}

and every where i want to i have to make a new object of
acount_bank, is it right or there is other ways to do it

thanks a lot.

If they are static there is no reason to create a "bank_account" object, simply reference the class (i.e. bank_account.search(whatever)). If your application is going to be threaded, however, you are going to have to synchronize those methods. A better solution in this case is probably a Singleton. Google for "Java Singleton Pattern" for information.

First of all write the code like this. Change the names of the classes to Capital first letter.

import java.util.*;

public class AcountBank {
    private  static Hashtable ht=new Hashtable();

 public static boolean  search(Information o){
   if(ht.containsKey(o)) return true ;return false;
 }
   public static void  add(Information o){
      ht.put(o, o);
  }

}

Since the methods and variable are static, you don't need to create a new Object. You do that for non static methods and variables. for the above:

AcountBank.add(...);
AcountBank.add(...);

boolean b = AcountBank.search(...);

Second for the above to work you will need to override the equals method in the Information class: Object.eqauls()

Third since you are using as key and value the same thing, the object Information, why don't you use a Vector. It has similar methods that you can use

hi

thanks for your answers,it was helpful,at least it wiped out some
questions,but let me tell you somthing ,my reason for useing hashtabels was to make a simple data base ,in wich i could search
for entriese ,by methods in hashtabel class(stupid,is`t it),but i did`t know any other ways to do this.(is the any predifiened classes that
can do this kind of works ,and help to make a data base),by the way what is the exact use of hashtabels?

thaks.

after i searched a lot,i found this:

javax.persistence.Query

i think this is the class for making data bases ,but i don`t know how it works,can u guys help me with this!

thanks

This article has been dead for over six months. Start a new discussion instead.