File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes external variable in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "external variable in java" Watch "external variable in java" New topic

external variable in java

arun singhal
Ranch Hand

Joined: Oct 25, 2009
Posts: 41
i am making a program in which i want to read a variable in a class ,that is set in other program and the variable is defined in some 3rd class.
just like we used extern in c++
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
There's no such thing as extern is Java. If whoever designed the other class wanted you to have access to a variable, they would have supplied it, most likely as a"get" method.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Just with your very first line, you can read the variable and its value of a different class by using its getter (getXXX()) method if it is an instance variable.

If it is a class-level, static variable you can access it directly by using the class name as a reference.

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Moreover, you should import the class name if it is a part of a different package than that of the one you are using it.
Henry Wong

Joined: Sep 28, 2004
Posts: 20535

If the variable in question, is a static variable of another class, you can do a "static import" which will make this external variable look like it was declared as a static variable of your class.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: external variable in java
It's not a secret anymore!