Publish Your Article Here

Program: Example for static variables and methods.


In java, static belongs to class. You can create static variables and static methods. You can call these directly by using class name, without creating instance.

Java static variables:

Static variables are belongs to the class and not to the object. These are only once, at the starting of the execution. Static variables are not part of object state, means there is only one copy of the values will be served to all instances. You can call static variable with reference to class name without creating an object. Below example shows how to create and call static variables.
Java static methods:

Static methods are also similar to static variables, you can access them with reference to class name, without creating object. Inside static methods, you cannot access instance variables or instance methods. You can only access static variables or static methods.

package com.java2novice.staticexmp;

public class MyStaticMethods {

	private String name;
	private static String staticStr = "STATIC-STRING";
	public MyStaticMethods(String n){ = n;
	public static void testStaticMethod(){
		System.out.println("Hey... I am in static method...");
		//you can call static variables here
		//you can not call instance variables here.
	public void testObjectMethod(){
		System.out.println("Hey i am in non-static method");
		//you can also call static variables here
		//you can call instance variables here
		System.out.println("Name: ";
	public static void main(String a[]){
		//By using class name, you can call static method
		MyStaticMethods msm = new MyStaticMethods("Java2novice");

Hey... I am in static method...
Hey i am in non-static method
Name: Java2novice
 Next Program >>

List Of All Static Keyword Programs:

  1. Example for static variables and methods.
  2. Example for static block.
  3. Example for static block vs constructor.
  4. Example for singleton class using static block.
Knowledge Centre
Different types of Access Modifiers
public: Any thing declared as public can be accessed from anywhere.

private: Any thing declared as private can't be seen outside of its class.

protected: Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages.

default modifier: Can be accessed only to classes in the same package.
Famous Quotations
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.