Java Static Keyword, Variables and Methods

In this tutorial, you will learn about static variables and methods in java programming.

You will learn what is static keyword, what is its use, how to create static variables and methods, how do they work, how do they differ from normal variables and methods in detail with example.

Source code for this video tutorial

Code for Parents.java

package oops;

public class Parents {
 String name;
}

Code for Student.java

package oops;

public class Student {
 public String name;
 public int age;
 public static int nostudents = 0;
 
 public Student(){
  nostudents++;
  System.out.println("This is the "+nostudents+"object created from this class");
 }
 
 public static void howManyStudents(){
  System.out.println("There are "+ nostudents+" students");
  
 }
 
 public void introduce(){
  System.out.println("Hey i'm "+name+" and i'm "+age+" years old");
  howManyStudents();
 }
 
}

Code for Tutorials.java

package oops;

public class Tutorials {

 public static void main(String[] args) {
  
  System.out.println(Student.nostudents);

  Student anil = new Student();
  Student shreesh = new Student();
  
  anil.name = "anil";
  anil.age = 24;
  
  shreesh.name = "Shreesh";
  shreesh.age = 25;
  
  anil.introduce();
  shreesh.introduce();
  
  Student.howManyStudents();
  
 }
}
Back To Top