Types of data members
-
Last Updated: July 26, 2023
-
By: javahandson
-
Series
In this article, we are going to learn about the types of data members in Java with proper examples.
Data members are the variables that we use within the class to store some value. Please check the article on variables and constants to understand this topic better.
Data members are classified into 2 types:
1. The Instance or Non-static data members
2. Static data members
Instance variable declaration will have a data type and the variable name.
Ex. int result;
We have to declare the instance variables within the Class. We cannot declare them inside the method or constructor or any block.
Memory is allocated to the instance data members when the Object is created.
Instance members are specific to the Object i.e. the memory is not shared between the instances. Every instance will have its own separate memory. For every Object creation, a new memory will be assigned.
package com.javahandson.oops; public class Student { int studentId; String name; public static void main(String[] args) { // New memory is assigned to instance variables of student1 Student student1 = new Student(); student1.studentId = 101; student1.name = "Rohit"; // New memory is assigned to instance variables of student2 Student student2 = new Student(); student2.studentId = 102; student2.name = "Virat"; System.out.println(student1); System.out.println(student2); } @Override public String toString() { return "Student{" + "studentId=" + studentId + ", name='" + name + '\'' + '}'; } } Output: Student { studentId = 101, name = 'Rohit' } Student { studentId = 102, name = 'Virat' }
In the above example, we can see student1 instance is having a different set of instance values from student2 because the memory assigned to student1 and student2 instances are different.
We can say every Class instance i.e. Object will have its own separate copy of instance variables. Instance data members are also known as Object-level data members because the memory is not shared between the Objects.
Instance data members should be accessed using the instance name. In the above example, we have accessed studentId and name variables using the student1 and student2 instance names.
The Instance variables can be accessed directly in the instance methods but to access them in static methods we need to create the Object.
package com.javahandson.oops; public class Student { int studentId; String name; public void setName() { name = "Rohit"; // we are able to access the instance variable directly } public static void main(String[] args) { // name = "Virat"; java: non-static variable name cannot be referenced from a static context Student student1 = new Student(); student1.studentId = 101; student1.setName(); Student student2 = new Student(); student2.studentId = 102; student2.name = "Virat"; System.out.println(student1); System.out.println(student2); } @Override public String toString() { return "Student{" + "studentId=" + studentId + ", name='" + name + '\'' + '}'; } } Output : Student{studentId=101, name='Rohit'} Student{studentId=102, name='Virat'}
In the above example, we are able to access the instance variable name in the instance block. But to access the variable name in the static block we have to create the Object else we get a compile time exception.
The static variable declaration will have a static keyword + data type + variable name.
Ex. static int result;
We have to declare the static variables within the Class. We cannot declare them inside the method or constructor or any block.
Static data members should be preceded by static keyword.
Memory is allocated to the static data members when the Class is loaded to the main memory.
Note* Class loader subsystem will load the byte code from secondary memory to the main memory. Class loader subsystem is a program developed by Sun Microsystems that is a part of Java software.
Static data members are not specific to the Objects. The memory is shared between the instances. The new memory will not be created for static members. The memory will be assigned to the data members only once at the time of Class loading.
package com.javahandson.oops; public class Student { static int studentId; String name; public static void main(String[] args) { Student student1 = new Student(); student1.studentId = 101; student1.name = "Rohit"; // In second instance we will not provide studentId. Since studentId is static hence it is shared Student student2 = new Student(); student2.name = "Virat"; System.out.println(student1); System.out.println(student2); } @Override public String toString() { return "Student{" + "studentId=" + studentId + ", name='" + name + '\'' + '}'; } } Output : Student{studentId=101, name='Rohit'} Student{studentId=101, name='Virat'}
In the above example, we can see the student1 instance sets the value of the static variable studentId. The same studentId value is reflected for the student2 instance as well because the static variable’s memory is sharable.
A single copy of variables is shared between all the Objects.
Static data members are used for storing common values that can be used by all instances.
We can access the static data members using the instance name as well as the Class name.
In the above example, we have accessed studentId using the student1 instance name. But in the below example, we will access studentId using the Student Class name.
package com.javahandson.oops; public class Student { static int studentId; String name; public static void main(String[] args) { // Accessing static data member using Class name Student.studentId = 101; Student student1 = new Student(); student1.name = "Rohit"; Student student2 = new Student(); student2.name = "Virat"; System.out.println(student1); System.out.println(student2); } @Override public String toString() { return "Student{" + "studentId=" + studentId + ", name='" + name + '\'' + '}'; } } Output : Student{studentId=101, name='Rohit'} Student{studentId=101, name='Virat'}
Static data members are also known as Class level data members because the memory is shared between the Objects.
We can access the static variables inside the instance block and static block directly using the Class name. We do not have to create an Object to access them.
package com.javahandson.oops; public class Student { static int studentId; static String name; public void setName() { name = "Rohit"; } public static void main(String[] args) { studentId = 101; Student student1 = new Student(); student1.setName(); Student student2 = new Student(); student2.setName(); System.out.println(student1); System.out.println(student2); } @Override public String toString() { return "Student{" + "studentId=" + studentId + ", name='" + name + '\'' + '}'; } } Output : Student{studentId=101, name='Rohit'} Student{studentId=101, name='Rohit'}
In the above example we are able to access the static variable studentId in the static block also we are able to access the static variable name in the instance block.
So this is about types of data members in Java. I hope you liked this article. If you have any questions on this topic please add it below in the comment section. Thank you for reading this article.