Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Access To Object Properties And Methods

In this tutorial, you will learn how to access an object's properties (member variables) and methods in Java. Let's start by creating a simple class called Person:

  • Create the Person class
public class Person {
    // Member variables (properties)
    private String name;
    private int age;

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getter methods (accessors)
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    // Setter methods (mutators)
    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // A custom method
    public String greet() {
        return "Hello, my name is " + name + " and I am " + age + " years old.";
    }
}
  • Accessing properties and methods

Now, let's create a Main class to demonstrate how to access the Person class's properties and methods:

public class Main {
    public static void main(String[] args) {
        // Create a new Person object
        Person person1 = new Person("John Doe", 25);

        // Access the object's properties using getter methods
        System.out.println("Name: " + person1.getName());
        System.out.println("Age: " + person1.getAge());

        // Modify the object's properties using setter methods
        person1.setName("Jane Doe");
        person1.setAge(30);

        // Access the object's properties again to see the changes
        System.out.println("Name: " + person1.getName());
        System.out.println("Age: " + person1.getAge());

        // Call the custom method
        System.out.println(person1.greet());
    }
}
  • Run the program

Compile and run the Main class, and you should see the following output:

Name: John Doe
Age: 25
Name: Jane Doe
Age: 30
Hello, my name is Jane Doe and I am 30 years old.

This demonstrates how to access an object's properties and methods in Java. In general, it is a good practice to use getter and setter methods to access and modify an object's properties, as it allows for better encapsulation and control over the data.

  1. Accessing Object Properties in Java:

    public class Person {
        public String name;
        public int age;
    }
    
    // Accessing object properties
    Person person = new Person();
    person.name = "John";
    person.age = 25;
    System.out.println(person.name);  // Output: John
    
  2. Accessing Object Methods in Java:

    public class Calculator {
        public int add(int a, int b) {
            return a + b;
        }
    }
    
    // Accessing object methods
    Calculator calculator = new Calculator();
    int result = calculator.add(5, 7);
    System.out.println(result);  // Output: 12
    
  3. Java Reflection Access Object Properties and Methods:

    // Assuming 'object' is an instance of some class
    Class<?> clazz = object.getClass();
    
    // Accessing properties
    Field field = clazz.getDeclaredField("propertyName");
    field.setAccessible(true);
    Object value = field.get(object);
    
    // Accessing methods
    Method method = clazz.getDeclaredMethod("methodName", parameterTypes);
    method.setAccessible(true);
    Object result = method.invoke(object, args);
    
  4. Using Reflection to Get and Set Object Fields in Java:

    Field field = clazz.getDeclaredField("propertyName");
    field.setAccessible(true);
    
    // Getting field value
    Object value = field.get(object);
    
    // Setting field value
    field.set(object, newValue);
    
  5. Java Reflection getDeclaredMethod() Method:

    Method method = clazz.getDeclaredMethod("methodName", parameterTypes);
    
  6. Java Reflection invokeMethod() Example:

    Method method = clazz.getDeclaredMethod("methodName", parameterTypes);
    Object result = method.invoke(object, args);
    
  7. Java Reflection Invoke Constructor and Methods:

    Constructor<?> constructor = clazz.getDeclaredConstructor(parameterTypes);
    Object instance = constructor.newInstance(args);
    
    Method method = clazz.getDeclaredMethod("methodName", parameterTypes);
    Object result = method.invoke(instance, args);