JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

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
Stream and types of Streams
A Stream is an abstraction that either produces or consumes information. There are two types of Streams and they are:

Byte Streams: Provide a convenient means for handling input and output of bytes. Byte Streams classes are defined by using two abstract classes, namely InputStream and OutputStream.

Character Streams: Provide a convenient means for handling input & output of characters. Character Streams classes are defined by using two abstract classes, namely Reader and Writer.
Famous Quotations
It is amazing what you can accomplish if you do not care who gets the credit.
-- Harry Truman

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.