JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Program: Example for static variables and methods.


Description:

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.


Code:
package com.java2novice.staticexmp;

public class MyStaticMethods {

	private String name;
	private static String staticStr = "STATIC-STRING";
	
	public MyStaticMethods(String n){
		this.name = n;
	}
	
	public static void testStaticMethod(){
		System.out.println("Hey... I am in static method...");
		//you can call static variables here
		System.out.println(MyStaticMethods.staticStr);
		//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
		System.out.println(MyStaticMethods.staticStr);
		//you can call instance variables here
		System.out.println("Name: "+this.name);
	}
	
	public static void main(String a[]){
		//By using class name, you can call static method
		MyStaticMethods.testStaticMethod();
		MyStaticMethods msm = new MyStaticMethods("Java2novice");
		msm.testObjectMethod();
	}
}

Output:
Hey... I am in static method...
STATIC-STRING
Hey i am in non-static method
STATIC-STRING
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
Difference between super() and this()
super() is used to call super class constructor, whereas this() used to call constructors in the same class, means to call parameterized constructors.
Famous Quotations
When I do good, I feel good; when I do bad, I feel bad, and that is my religion.
-- Abraham Lincoln

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 © 2020 by Nataraja Gootooru. All Rights Reserved.