

How to Access Variable From Another Class in Java?
source link: https://www.thejavaprogrammer.com/how-to-access-variable-from-another-class-in-java/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this article, we will learn how to access variable from another class in java.
There are two ways to get variables from another class.
- Create an object of another class in the main class
- Extend another class in the main class
Let us take a look at both these methods one by one with the help of sample programs.
Method 1: Create Object of Another Class in Main Class
In the following example, to access the variable ‘a’ of class A, we create its object in another class B. After that, we use this object to use the value of variable ‘a’ in class B. Note that if we do not initialize this variable, then its default value is taken to be zero.
Syntax:
Program:
Output:
Method 2: Extend Another Class in Main Class
If we wish to access a variable from another class, we have a keyword, ‘extends’, which we can use in the child class. Using it, the child class can inherit all the properties of the parent class.
Syntax:
Program:
Output:
In the above program, we created two classes, namely ‘A’ and ‘B’. In class ‘A’, we defined an instance variable ‘a’ with a value of 10. The other class ‘B’, extends the class ‘A’ using the ‘extends’ keyword. So, it can use the instance variable of class ‘A’ by creating its object. So, the child class inherits all the properties of its parent class.
If we wish to use an instance variable, then we have to use objects to call them. Every time you create a new object from a class, you get a new copy of each of the class’s instance variables. These copies are associated with the new object. So, each and every instance variable is accessed by the object.
Program:
Output:
In the above example, the default value of a is 0 and we updated its value to 3 using an object.
So, that’s all about accessing variables from another class.
Recommend
-
18
An example of a subtle bug that you can run into in C and C++: accidentally overwriting another local variable. Such an issue can manifest only on certain systems, and can be hard to debug if you have not seen it before. A...
-
13
How to pass an integer variable to another class advertisements I have one class with public int Result; Later in the game I use that variable...
-
8
Where is a private instance variable of an abstract class created in the heap? advertisements abstract class A { private int a;...
-
8
How to match arraylist data to another class in java advertisements I developed a student class which add students, and in the main class I used ar...
-
7
Referencing a variable in another function? advertisements I'm an experienced TCL developer and write my own procedures to help myself along....
-
4
Do you reference another sheet per variable cell? advertisements What I am trying to do is to create a formula which references another sheet....
-
14
Java ActionListener in another class - access objects in the main class advertisements i am writing a simple BMI calculator program. The applicatio...
-
5
AS3 - Class root access variable advertisements How to access a variable in the root timeline from within a class? There is a variable called
-
8
The variable in this class does not name a C ++ type advertisements I am trying to use log4cpp in my program (*nix machine...
-
5
Java local variable vs class field initialization and default values This is a common error when starting out with Java: DefaultValues.java:5:...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK