Posts

Showing posts from February, 2024

Java static keyword part 2

Image
   Hello friends, we have discussed about java static variables in the previous post. Now we are going to learn static methods.  When we apply static keyword with a method, it will be a static method.       A static method belongs to the class rather than the object of a class. It can be called without creating an object. It can access static data member and can change its value. Let's look at a small example.  Let's consider another example. class Employee{ int empId ; String name ; static String company = "ZTE" ; static void resign (){ company = "Motorolla" ; } Employee ( int e, String n){ empId = e; name = n; } void PrintDetails (){ System. out .println( empId + " " + name + " " + company ); } } class Main { public static void main (String[] args){ Employee. resign (); Employee nimal = new Employee( 101 , "Nimal Perera" ); Emp...