Paramclasses Academy - Blog

Tutorials Details

←
Post 7 of 40
→

Java Methods, Classes and Objects Explained for Android App Development ?

Java Methods, Classes and Objects Explained for Android App Development ?

November 2, 2026

1. What Is a Method in Java?

A method is a block of code that performs a specific task.

Instead of writing the same code repeatedly, we can create a method once and call it whenever required.

For example, if we want to display a welcome message multiple times, we can create a method for it.

Syntax:

returnType methodName() {
    // Code to execute
}

Example:

public class Main {

    static void welcome() {
        System.out.println("Welcome to Param Classes");
    }

    public static void main(String[] args) {

        welcome();
        welcome();
    }
}

Output:

Welcome to Param Classes
Welcome to Param Classes

Explanation:

  • static means the method belongs to the class and can be called without creating an object.

  • void means the method does not return any value.

  • welcome() is the method name.

  • System.out.println() displays the message.

  • welcome() inside main() calls the method.

The main advantage of methods is code reusability. We can write the logic once and use it multiple times.

2. Types of Methods in Java

Java methods can be created in different ways depending on whether they accept parameters and return values.

Let's understand the four common combinations.

A. Method Without Parameters and Without Return Value

This method does not accept any input and does not return any result.

public class Main {

    static void display() {
        System.out.println("Hello Java");
    }

    public static void main(String[] args) {

        display();
    }
}

Output:

Hello Java

Here, display() performs a task but does not receive any value or return a result.

B. Method With Parameters and Without Return Value

A parameter allows us to pass information into a method.

public class Main {

    static void greet(String name) {
        System.out.println("Hello " + name);
    }

    public static void main(String[] args) {

        greet("Rahul");
        greet("Amit");
    }
}

Output:

Hello Rahul
Hello Amit

Explanation:

  • String name is the method parameter.

  • "Rahul" and "Amit" are arguments passed when calling the method.

  • The method uses the received name to display a message.

C. Method Without Parameters and With Return Value

This method does not accept input but returns a result.

public class Main {

    static int getNumber() {
        return 100;
    }

    public static void main(String[] args) {

        int result = getNumber();

        System.out.println(result);
    }
}

Output:

100

Explanation:

  • int indicates that the method returns an integer.

  • return 100 sends the value back to the calling code.

  • result stores the returned value.

D. Method With Parameters and With Return Value

This method accepts input, performs an operation, and returns the result.

public class Main {

    static int add(int a, int b) {

        return a + b;
    }

    public static void main(String[] args) {

        int result = add(10, 20);

        System.out.println("Sum = " + result);
    }
}

Output:

Sum = 30

Explanation:

  • add(int a, int b) accepts two integer values.

  • return a + b calculates their sum and returns it.

  • add(10, 20) passes 10 and 20 to the method.

  • The returned result is stored in the result variable.

This type of method is useful when we want to perform calculations or process data.

3. What Is a Class in Java?

A class is a blueprint or template used to create objects.

It defines the properties and behaviors that objects of that class can have.

For example, imagine a student management application. Every student may have:

  • Name

  • Age

  • Roll number

  • A method to display student details

We can define these properties and behaviors inside a Student class.

Example:

class Student {

    String name;
    int age;

    void displayDetails() {

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

Explanation:

  • class Student creates a class named Student.

  • String name stores the student's name.

  • int age stores the student's age.

  • displayDetails() displays the student's information.

A class defines what its objects can contain and do. It does not automatically create a student object.

4. What Is an Object in Java?

An object is an instance of a class.

We use objects to access the non-static variables and methods defined inside a class.

Syntax:

ClassName objectName = new ClassName();

Example:

class Student {

    String name;
    int age;

    void displayDetails() {

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

public class Main {

    public static void main(String[] args) {

        Student s1 = new Student();

        s1.name = "Rahul";
        s1.age = 21;

        s1.displayDetails();
    }
}

Output:

Name: Rahul
Age: 21

Explanation:

  • Student is the class name.

  • s1 is the object reference variable.

  • new Student() creates a new Student object.

  • s1.name assigns a name to the object.

  • s1.age assigns an age.

  • s1.displayDetails() calls the object's method.

Think of a class as a blueprint of a house and an object as an actual house created using that blueprint.

5. Creating Multiple Objects From One Class

One class can be used to create multiple objects.

Each object can store its own data.

Example:

class Student {

    String name;
    int age;

    void displayDetails() {

        System.out.println(name + " - " + age);
    }
}

public class Main {

    public static void main(String[] args) {

        Student s1 = new Student();

        s1.name = "Rahul";
        s1.age = 21;

        Student s2 = new Student();

        s2.name = "Amit";
        s2.age = 22;

        s1.displayDetails();
        s2.displayDetails();
    }
}

Output:

Rahul - 21
Amit - 22

Explanation:

  • s1 and s2 are two separate objects.

  • Both objects are created from the same Student class.

  • Each object stores different name and age values.

  • Changing the data of one object does not automatically change the other.

This approach is useful when an application needs to manage multiple students, employees, products, or users.

6. What Is a Constructor in Java?

A constructor is a special member of a class that is called when an object is created.

It is commonly used to initialize object data.

A constructor has the same name as its class and does not have a return type.

Example:

class Student {

    String name;
    int age;

    Student(String studentName, int studentAge) {

        name = studentName;
        age = studentAge;
    }

    void displayDetails() {

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

public class Main {

    public static void main(String[] args) {

        Student s1 = new Student("Rahul", 21);

        s1.displayDetails();
    }
}

Output:

Name: Rahul
Age: 21

Explanation:

  • Student() is the constructor.

  • It accepts a name and age.

  • When new Student("Rahul", 21) executes, the constructor is called.

  • The constructor assigns the received values to the object's variables.

Important: If you do not write any constructor, Java can provide a default no-argument constructor. But when you declare your own constructor, Java does not automatically provide that default constructor.

7. What Is the Difference Between Method and Constructor?

A method performs a particular task, while a constructor initializes a newly created object.

For example:

  • A constructor can initialize a student's name and age.

  • A method can display the student's details.

Remember these differences:

  • A constructor has the same name as the class.

  • A method can have any valid method name.

  • A constructor has no return type.

  • A method has a return type, such as void, int, or String.

  • A constructor is called when an object is created.

  • A method is called when its functionality is needed.

8. What Is the this Keyword in Java?

The this keyword refers to the current object.

It is commonly used when instance variables and constructor parameters have the same names.

Example:

class Student {

    String name;
    int age;

    Student(String name, int age) {

        this.name = name;
        this.age = age;
    }

    void displayDetails() {

        System.out.println(name);
        System.out.println(age);
    }
}

public class Main {

    public static void main(String[] args) {

        Student s1 = new Student("Rahul", 21);

        s1.displayDetails();
    }
}

Explanation:

  • this.name refers to the current object's instance variable.

  • name refers to the constructor parameter.

  • this.age refers to the current object's age variable.

  • age refers to the constructor parameter.

Without this, Java may not distinguish between the instance variable and the parameter when they have the same name.

9. What Is the static Keyword in Java?

The static keyword is used for members that belong to the class rather than individual objects.

A static method or variable can be accessed using the class name without creating an object.

Example:

class Calculator {

    static int add(int a, int b) {

        return a + b;
    }
}

public class Main {

    public static void main(String[] args) {

        int result = Calculator.add(10, 20);

        System.out.println(result);
    }
}

Output:

30

Explanation:

  • static int add() defines a static method.

  • Calculator.add() calls the method using the class name.

  • We do not need to create a Calculator object.

Important: A static method cannot directly access non-static instance variables or methods. It needs an object to access them.

10. How Classes and Objects Are Used in Android Development

Classes and objects are important parts of Android application development.

For example, suppose we are creating a student management application.

We can create a Student class to store student information.

Student.java

public class Student {

    String name;
    int age;
    String course;

    public Student(String name, int age, String course) {

        this.name = name;
        this.age = age;
        this.course = course;
    }

    public String getStudentDetails() {

        return "Name: " + name +
               "\nAge: " + age +
               "\nCourse: " + course;
    }
}

Now we can use this class inside an Android Activity.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = findViewById(R.id.textView);

        Student student = new Student(
            "Rahul",
            21,
            "Android Development"
        );

        textView.setText(student.getStudentDetails());
    }
}

Explanation:

  • Student.java defines the structure of student data.

  • Student student creates a reference variable.

  • new Student(...) creates a Student object.

  • The constructor initializes the student's information.

  • getStudentDetails() returns the formatted student details.

  • textView.setText() displays those details on the Android screen.

For this example, create a TextView with the ID textView in activity_main.xml and ensure the Activity has the required AndroidX imports.

This approach keeps the student data separate from the screen-related code, making the application easier to organize and maintain.

11. Common Mistakes Beginners Should Avoid

1. Calling a non-static method without an object

Incorrect:

class Student {

    void display() {
        System.out.println("Hello");
    }
}

public class Main {

    public static void main(String[] args) {

        display();
    }
}

Correct:

Student s1 = new Student();
s1.display();

2. Giving a constructor a return type

Incorrect:

void Student() {
    System.out.println("Constructor");
}

This is a method, not a constructor.

Correct:

Student() {
    System.out.println("Constructor");
}

3. Forgetting to create an object

If a method or variable is non-static, you generally need an object to access it from a static context.

4. Confusing class and object

A class defines the structure. An object is an instance created from that class.

5. Forgetting return statements

If a method declares a return type such as int or String, it must return an appropriate value on every possible execution path.

12. Practice Programs

Try writing these programs yourself in Java.

  1. Create a method that displays your name.

  2. Create a method that accepts two numbers and returns their sum.

  3. Create a method that checks whether a number is even or odd.

  4. Create a Student class with name, age, and roll number.

  5. Create two Student objects with different information.

  6. Create an Employee class with a constructor to initialize employee details.

  7. Create a Calculator class with methods for addition, subtraction, multiplication, and division.

  8. Create a static method that calculates the square of a number.

  9. Create a Product class with a constructor and a method to display product details.

  10. Create a simple Student class and display its details inside an Android TextView.

Try to understand why each method, class, and object is needed instead of memorizing the code.

13. Conclusion

Methods, classes, and objects are fundamental concepts of Java programming.

A method is used to perform a specific task. A class defines the structure and behavior of objects. An object is an instance of a class that stores its own data and uses the class's methods.

These concepts help developers write reusable, organized, and maintainable code.

In Android development, classes and objects are used to manage application data, organize functionality, and separate different responsibilities within an application.

Practice the examples in Android Studio to build a strong foundation for the next Java concepts.

14. Frequently Asked Questions (FAQs)

Q1. What is a method in Java?

A method is a block of code that performs a specific task. It can be called whenever its functionality is required.

Q2. What is a class in Java?

A class is a blueprint that defines the variables and methods that its objects can use.

Q3. What is an object in Java?

An object is an instance of a class. It can store data and access the methods defined by its class.

Q4. What is the difference between a class and an object?

A class defines the structure, while an object is an actual instance created using that structure.

Q5. What is a constructor in Java?

A constructor is a special member that is called when an object is created. It is commonly used to initialize object data.

Q6. Can a Java class have multiple methods?

Yes. A Java class can contain multiple methods to perform different tasks.

Q7. What is the use of the this keyword?

The this keyword refers to the current object. It is commonly used to distinguish instance variables from parameters with the same names.

Q8. What is the use of static in Java?

The static keyword makes a member belong to the class rather than individual objects.

Q9. Why are classes and objects important in Android development?

They help organize application data and functionality, making Android applications easier to develop, maintain, and extend.

15. SEO Details

SEO Title: Java Methods, Classes and Objects Explained for Android Development

Meta Description: Learn Java methods, classes, objects, constructors, this keyword, and static keyword with practical examples for Android Studio and Java beginners.

Focus Keyword: Java Methods Classes and Objects

Related Keywords:

  • Java methods tutorial

  • Java classes and objects

  • Java constructor

  • Java this keyword

  • Java static keyword

  • Object-oriented programming in Java

  • Java programming for Android

  • Android Studio Java tutorial

Suggested URL Slug: java-methods-classes-objects-android-development

←
Post 7 of 40
→