Suppose, I've a class TestA
which has variable int Age
like -
class TestA{
int Age = 25;
}
and I want to access it from class TestB
without using class name(in this case, I'll have to declare Age
as static
) and without creating object of TestA
How can we achieve this in Java?
Note: TestB
is not subclass of TestA
Define that field as static and do something like:
import static TestA.Age;
class TestB {
public void test() {
System.out.println(Age);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.