Class Members in Java
-
Last Updated: February 16, 2025
-
By: javahandson
-
Series
Learn about class members in Java, including fields (instance variables), methods, constructors, static variables, static methods, and nested classes. Understand their roles, usage, and key differences with clear explanations and examples.
Class members are the components or elements that make up a class. They define a class’s state (data) and behavior (functions). In Java, class members include:
Instance variables hold an object’s state and represent its data. These variables are specific to each object, meaning every object has its own copy of the instance variables.
class Student { // Instance variables (Fields) String name; int age; String grade; }
In this example, name, age, and grade are instance variables of the Student class.
Methods define the behavior of objects. They specify what actions an object can perform. Methods can access and modify instance variables (fields) of the class.
package com.java.handson.members; class Student { String name; int age; // Method to display student details void displayStudentInfo() { System.out.println("Name: " + name); System.out.println("Age: " + age); } }
Here, displayStudentInfo() is a method that prints the Student object’s details.
package com.java.handson.members; public class Main { public static void main(String[] args) { Student student = new Student(); student.name = "Suraj"; student.age = 34; student.displayStudentInfo(); } } Output: Name: Suraj Age: 34
Constructors are special methods used to initialize objects when they are created. They have the same name as the class and do not return any value (not even void). There are two types of constructors:
package com.java.handson.members; public class Student { String name; int age; // Parameterized Constructor Student(String name, int age) { this.name = name; this.age = age; } // Method to display student details void displayStudentInfo() { System.out.println("Name: " + name); System.out.println("Age: " + age); } }
In this example, the Student class has a parameterized constructor that initializes the name and age when the object is created.
package com.java.handson.members; public class Main { public static void main(String[] args) { Student student = new Student("Suraj", 34); student.displayStudentInfo(); } } Output: Name: Suraj Age: 34
Static variables belong to the class rather than to any specific object. They are shared across all instances (objects) of the class. Static variables are initialized only once, and all objects of the class share the same copy.
package com.java.handson.members; public class Student { static int numberOfStudents = 0; // Static variable String name; // Constructor Student(String name) { this.name = name; numberOfStudents++; // Increment static variable } // Method to display student details void displayStudentInfo() { System.out.print("Name: " + name + " && "); System.out.println("Number of students: " + numberOfStudents); } }
Here, the numberOfStudents is a static variable, and it is shared by all instances of the Student class.
package com.java.handson.members; public class Main { public static void main(String[] args) { Student student1 = new Student("Suraj"); student1.displayStudentInfo(); Student student2 = new Student("Shweta"); student2.displayStudentInfo(); Student student3 = new Student("Ranjitha"); student3.displayStudentInfo(); } } Output: Name: Suraj && Number of students: 1 Name: Shweta && Number of students: 2 Name: Ranjitha && Number of students: 3
As the numberOfStudents is a static variable hence it shares the same copy for all the Student objects and it keeps on incrementing.
Static methods belong to the class, not to instances (objects). They can access only static variables and call other static methods. However, static methods can access instance variables and call instance methods, but only through an object reference. Static methods are called using the class name or through any object of the class.
package com.java.handson.members; public class Student { static int numberOfStudents = 0; // Static variable String name; // Static method to get the number of students static int getNumberOfStudents() { return numberOfStudents; } // Constructor Student(String name) { this.name = name; numberOfStudents++; // Increment static variable } }
Here, getNumberOfStudents() is a static method that accesses the static variable numberOfStudents.
package com.java.handson.members; public class Main { public static void main(String[] args) { Student student = new Student("Suraj"); System.out.println("Name : "+student.name); System.out.println("Number of students : "+student.getNumberOfStudents()); } } Output: Name : Suraj Number of students : 1
Here, the main is a static method that accessed the name instance variable only through student reference.
Nested classes are defined inside another class. There are two types of nested classes in Java:
Inner classes (non-static, can access instance members) – An inner class is a class defined inside another class. It has access to the outer class’s members, including private fields.
Static nested classes (can access only static members of the outer class) – It does not require an instance of the outer class and behaves more like a normal top-level class, except that it is nested inside another class.
package com.java.handson.members; public class Student { private String name; private int rollNumber; // Constructor public Student(String name, int rollNumber) { this.name = name; this.rollNumber = rollNumber; } // Non-static Inner Class class Address { private String city; private String state; // Constructor public Address(String city, String state) { this.city = city; this.state = state; } public void displayAddress() { System.out.println("Student: " + name + " (Roll No: " + rollNumber + ")"); System.out.println("Address: " + city + ", " + state); } } // Static Inner Class static class School { private static String schoolName = "MCC High School"; public static void displaySchool() { System.out.println("School Name: " + schoolName); } } public static void main(String[] args) { // Creating Student object Student student = new Student("Suraj", 101); // Creating an instance of the non-static inner class Student.Address address = student.new Address("Chandrapur", "Maharashtra"); address.displayAddress(); // Calling static inner class method Student.School.displaySchool(); } } Output: Student: Suraj (Roll No: 101) Address: Chandrapur, Maharashtra School Name: MCC High School
In the above example, the Student class represents a student with a name and roll number. The Inner class Address represents the student’s address. Since it is a non-static inner class, it can access the Student instance fields. The Static Inner class School contains details about the school. Since it is static, it does not depend on any Student instance.
Understanding class members in Java is crucial for writing efficient and well-structured code. Fields, methods, and constructors define an object’s state and behavior, while static members provide shared functionality. Nested classes offer additional organization and encapsulation. By mastering these concepts, you can design more robust and maintainable Java applications.
So this is all about class members in Java. If you have any questions on this topic, please raise them in the comments section. If you liked this article then please share this post with your friends and colleagues.