DEV Community

Sugumar R
Sugumar R

Posted on

Day 4: Methods,static & Non static.

How to Call an Object in Java?

In Java, we call or access an object by:

  1. Creating an object of a class using the new keyword.

  2. Calling methods and variables using the object.

Syntax to Create an Object

$ ClassName objectName = new ClassName();

$ ClassName → Name of the class.

$ objectName → Name of the object.

$ new → Allocates memory and creates the object.

$ ClassName() → Calls the constructor of the class.

Example: Object Creation and Method Calling

class Student {
    void display() {  // Method
        System.out.println("Hello, I am a Student!");
    }

    public static void main(String[] args) {
        Student obj = new Student();  // Object creation
        obj.display();  // Calling method using object
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, I am a Student!

Example: Calling Instance Variables and Methods

class Car {
    String brand = "Toyota";  // Instance variable

    void showBrand() {  // Method
        System.out.println("Car Brand: " + brand);
    }

    public static void main(String[] args) {
        Car myCar = new Car();  // Creating object
        System.out.println(myCar.brand);  // Accessing variable
        myCar.showBrand();  // Calling method
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Toyota
Car Brand: Toyota

What is method?

just like have you call o another phone. Using phone number you can call other function using function name.

Why Use Methods?

define the code once,and use it many times.

$ Code Reusability – Write once, use multiple times.

$ Improves Readability – Organizes code into small, logical parts.

$ Reduces Redundancy – Avoids writing the same code repeatedly.

$ Easier Debugging – Makes it easy to find and fix errors.

Syntax of a Method

returnType methodName(parameters) {
    // method body
    return value;  // If returnType is not void
}
Enter fullscreen mode Exit fullscreen mode

returnType– Specifies the type of value the method returns (int, String, void, etc.).

methodName – The name of the method.

parameters – Input values (optional).

return– Used to return a value (optional for void methods).

Example 1: Method Without Parameters and Return Type

class Example {
    void greet() {  // No parameters, no return
        System.out.println("Hello, Welcome!");
    }

    public static void main(String[] args) {
        Example obj = new Example();
        obj.greet();  // Calling the method
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, Welcome!

Calling Static and Non-Static Methods

1. Static

$ Declared using the static keyword.

$ Belongs to the class rather than a specific instance.

$ Shared among all objects of the class.

$ Can be accessed without creating an object.

$ Cannot access non-static members directly.

Calling Static Methods (Without Object)

class Example {
    static void show() {  // Static method
        System.out.println("Static Method Called");
    }

    public static void main(String[] args) {
        Example.show();  // Calling static method
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Static Method Called

2. Non-Static (Instance)

$ Does not use the static keyword.

$ Belongs to a specific object of the class.

$ Each object has its own copy of instance variables.

$ Can access both static and non-static members.

Calling Non-Static Methods (Using Object)

class Example {
    void show() {  // Non-static method
        System.out.println("Non-Static Method Called");
    }

    public static void main(String[] args) {
        Example obj = new Example();  // Creating object
        obj.show();  // Calling non-static method
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Non-Static Method Called

Key Points

✅ Static methods can be called without an object.
✅ Non-static methods and variables require an object to be accessed.
✅ new keyword is used to create an object in Java.

Global and Local Variables in Java

In Java, variables can have different scopes based on where they are declared. The two main types are:

  1. Global Variables (Instance/Class Variables)

  2. Local Variables

1. Global Variables (Instance or Class Variables)

Declared outside of methods, inside the class.

Can be accessed by all methods in the class.

Can be instance variables (different for each object) or static variables (shared among all objects).

Example: Global (Instance) Variable

class Example {
    int num = 10;  // Global variable (Instance variable)

    void display() {
        System.out.println("Number: " + num);  // Accessible in all methods
    }

    public static void main(String[] args) {
        Example obj = new Example();
        obj.display();
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:

Number: 10

Example: Global (Static) Variable

class Example {
    static int count = 5;  // Global static variable

    public static void main(String[] args) {
        System.out.println("Count: " + count);  // No need to create an object
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:

Count: 5

2. Local Variables

$ Declared inside a method, constructor, or block.

$ Can be accessed only within the method where they are declared.

$ Cannot have static keyword.

Example: Local Variable

class Example {
    void show() {
        int x = 20;  // Local variable
        System.out.println("Value of x: " + x);
    }

    public static void main(String[] args) {
        Example obj = new Example();
        obj.show();
        // System.out.println(x);  // Error! 'x' is not accessible here
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:

Value of x: 20

Key Differences Between Global and Local Variables.

Image of Stellar post

How a Hackathon Win Led to My Startup Getting Funded

In this episode, you'll see:

  • The hackathon wins that sparked the journey.
  • The moment José and Joseph decided to go all-in.
  • Building a working prototype on Stellar.
  • Using the PassKeys feature of Soroban.
  • Getting funded via the Stellar Community Fund.

Watch the video

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay