DEV Community

silambarasan rajendran
silambarasan rajendran

Posted on

2

Day-15: Static vs Non-Static Methods, Global vs Local Variables in Java

Static Keyword in Java:
The static keyword in Java is used to create class-level variables and methods. These members are shared across all instances of the class, meaning they are class-wide and not tied to individual object instances.

Static Variables (Fields)
Static variables, also referred to as class variables or fields, are shared among all instances of a class. These variables are initialized only once and are common to every object of that class.

Static Methods
Static methods can be called without creating an instance of the class. These methods can only access other static members (variables and methods) within the class.

Example code for Static and Local Variables in Java:

public class Clock {
    // Static
    static String timezone = "IST";
    static void setAlarm() {
        System.out.println(" Alarm set for 7:00 AM!");
    }

    // Non-static - decleartion
    int hours;
    int minutes;

    void displayTime() {
        String greeting; // Local variable
        if (hours < 12) greeting = "Good morning!";
        else if (hours < 18) greeting = "Good afternoon!";
        else greeting = "Good evening!";

        System.out.println(greeting + " Current time: " + hours + ":" + minutes + " " + timezone);
    }

    public static void main(String[] args) {
        // Static access
        System.out.println("Timezone: " + Clock.timezone);
        Clock.setAlarm();

        // Time objects
        Clock t1 = new Clock();
        t1.hours = 9;
        t1.minutes = 30;


        // Non-static calls
        t1.displayTime();
    }
}
Enter fullscreen mode Exit fullscreen mode

Code Output:

Image description

Note: The document formatting and structure were assisted by ChatGPT.

--------------------------- End of the Blog -------------------------------

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️