Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

external variable in java

 
arun singhal
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Marshal
Pie
Posts: 20998
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic