2.8. Static Members

Static members only belong to the class, and are not part of object created from the class.

Static variable: Static variable belongs to the class, and not to any object of the class. A static variable is initialized when the class is loaded at runtime

Static methods: Static methods that belong to the class, and not to any specific objects of the class. Also, you can directly invoke the method using class name and without creating an instance of the class.

A bad programming practice, don’t do it: Static methods can also be accessed via object reference. Let's build a brand new PaintFactory class to learn about Static members in Java.

			package javatutorial;

			/**
			 * Example class demonstrating static members of the class
			 * 
			 * @author f5java.com
			 *
			 */
			public class PaintFactory {
				
				// 1. static fields of the class : belongs to the class
				static String factoryName = "GoodPaints";
				static String paintBoxSize = "1 litre";
				
				// 2. static method of the class : belongs to the class
				public static void displayDefaultFactoryDetails(){
					System.out.println("Factory Name: " + factoryName);
					System.out.println("Paint box size: " + paintBoxSize);
				}
				
				// 3. instance field of the class : belongs to the object
				int customerId = 1;
				String customername = "Susie";
				int orderSize = 100;
				
				// 4. instance method of the class : belongs to the object
				public void displayCustomerOrder(){
					System.out.println("Customer Id: " + customerId);
					System.out.println("Customer Name: " + customername);
					System.out.println("Customer order size: " + orderSize);
				}

			}
		

Another class StaticMemberTester is created to access static and instance members of PaintFactory class

			package javatutorial;

			/**
			 *Example class showing Static members access
			 *
			 *@author  f5java.com
			 */
			public class StaticMemberTester {


				public static void main(String[] args) {
					
					// 1. static members belong to class, So the static fields and methods
					// can be directly accessed without creating an object
					System.out.println("PaintFactory Name: " + PaintFactory.factoryName);
					System.out.println("Paint box size: " + PaintFactory.paintBoxSize);
					
					// 2. Accessing static methods without instantiation
					PaintFactory.displayDefaultFactoryDetails();
					
					// 3. You cannot access instance members without instantiation, for eg. you cannot
					// invoke displayCustomerOrder method just using PaintFactory class
					
					// 4. Accessing instance methods and fields
					PaintFactory paintFactory = new PaintFactory();
					
					// 5. calling instance method
					paintFactory.displayCustomerOrder();
					
					// 6. display instance fields
					System.out.println("Customer Id: " + paintFactory.customerId);
					System.out.println("Customer Name: " + paintFactory.customername);
					System.out.println("Customer Order: " + paintFactory.orderSize);

				}

			}