What is Static Keyword in Java
What is Static Keyword in Java? In this Article i will step through the sample code that created by me to learn about the static keyword
What is a Static Keyword?
- Any property that you want all your instance or object to share, you declare it as “Static”.
- For example you have class named Dog, all dog barks right, so you would want to declare the “private static String dogSound ” as static. Unless one of your dog meow, just kidding.
- Direct access the static member without creating a class instance.So conclusion Static Variable is a member of the Class they are not a member of the Instance or object.
In this Code Example below , i will create 2 Classes, the first one is the class of Dog, and the other is a class to access the Object.
Let Step through the code to learn what is Static Keyword in Java
- First i created a class call “StaticExample”
- In this class i declared All variable as private since the variable is only reserve for this class only
- I declare private static String dogSound =”Barking”;as Static as i want this property to be share by all instance or object. Everyone knows dog barks
- I created 2 Constructor, the first one is the constructor which provide the default value, when no value is set
- The Second Constructor is where the user can set the value of the ” StaticExample Class “
- Declare all the Getter Setter for the Private Variable , to learn about Getter Setter Click here
- We have done our StaticExample class , let create another class call StaticDemo to access it
- First importjava.util.* we are going to use the scanner in this example
- Create the Static String Variable for createDog; assignDogInstance; newdogname; newbreed; newcolor; we will pass the input scanner value into this variable later in the program below
- Create 5 new Scanner Object
- At this Moment we have not created an Object or instance yet
- Some Alien is asking how Does Dog sound like?
- Since the dogsound varible is declare as static in the StaticExample Class We can now direct access the variable information through the class name StaticExample.getDogSound()
- The Alien Want to create a new Dog with a DNA if the input is “y” the program will create a new dog object.
- The Alien will input all the Dog variable value
- All the Dog Variable value will be passed on through the constructor StaticExample(assignDogInstance ,newdogname ,newbreed , newcolor);
- The Last step is to print out all the value enter
package staticpack; public class StaticExample { private String dogName; private String dogColor; private String dogBreed; private static String dogSound ="Barking"; private String objName ; public StaticExample(){ this.dogName = "wangwang"; this.dogBreed = "labrador"; this.dogColor = "brown"; //This is the Default Dog Feature set by the Constructor } public StaticExample(String objName ,String dogName ,String dogBreed , String dogColor ){ this.objName = objName; this.dogName = dogName; this.dogBreed = dogBreed; this.dogColor = dogColor; } public String getDogName() { return dogName; } public void setDogName(String dogName) { this.dogName = dogName; } public String getDogColor() { return dogColor; } public void setDogColor(String dogColor) { this.dogColor = dogColor; } public String getDogBreed() { return dogBreed; } public void setDogBreed(String dogBreed) { this.dogBreed = dogBreed; } public static String getDogSound() { return dogSound; } public static void setDogSound(String dogSound) { StaticExample.dogSound = dogSound; } public String getObjName() { return objName; } public void setObjName(String objName) { this.objName = objName; } }
package staticpack; import java.util.*; public class StaticDemo { static String createDog; static String assignDogInstance; static String newdogname; static String newbreed; static String newcolor; static Scanner inputscan1 = new Scanner(System.in); static Scanner inputscan2 = new Scanner(System.in); static Scanner inputscan3 = new Scanner(System.in); static Scanner inputscan4 = new Scanner(System.in); static Scanner inputscan5 = new Scanner(System.in); public static void main(String[] args) { System.out.println("Hi i am from Mar i am an Alien! how Does Dog sound like "); System.out.println(StaticExample.getDogSound()); //Direct access the static member without creating a class instance. System.out.println("I want to create a new dog with its Sample DNA . Press (y) to proceed "); createDog = inputscan1.nextLine(); if(createDog.contentEquals("y")){ System.out.println("What is the name of the new Dog you want to create"); assignDogInstance = inputscan2.nextLine(); System.out.println("What is the name of the Dog"); newdogname = inputscan3.nextLine(); System.out.println("What is the Dog Breed"); newbreed = inputscan4.nextLine(); System.out.println("What is the Dog color"); newcolor = inputscan5.nextLine(); StaticExample InputDog = new StaticExample(assignDogInstance ,newdogname ,newbreed , newcolor); System.out.println(InputDog.getObjName()); System.out.println(InputDog.getDogName()); System.out.println(InputDog.getDogBreed()); System.out.print(InputDog.getDogColor()); } } }
Check out Oracle Java Library here there are tonnes of information you need as a Java Developer
Leave a Reply