Saturday, April 4, 2015

Java Static Method and Java Constructors tutorial

Java static method

Java static method program: static methods in Java can be called without creating an object of class. Have you noticed why we write static keyword when defining main it's because program execution begins from main and no object has been created yet. Consider the example below to improve your understanding of static methods.

Java static method example program

class Languages {
  public static void main(String[] args) {
    display();
  }
 
  static void display() {
    System.out.println("Java is my favorite programming language.");
  }
}
Output of program:
Java static method program

Java static method vs instance method

Instance method requires an object of its class to be created before it can be called while static method doesn't require object creation.
class Difference {
 
  public static void main(String[] args) {
    display();  //calling without object
    Difference t = new Difference();
    t.show();  //calling using object
  }
 
  static void display() {
    System.out.println("Programming is amazing.");
  }
 
  void show(){
    System.out.println("Java is awesome.");
  }
}
Output of code:
Static vs instance method program

Using static method of another classes

If you wish to call static method of another class then you have to write class name while calling static method as shown in example below.
import java.lang.Math;
 
class Another {
  public static void main(String[] args) {
    int result;
 
    result = Math.min(10, 20); //calling static method min by writing class name
 
    System.out.println(result);
    System.out.println(Math.max(100, 200));
  }
}
Output of program:
10
200
Here we are using min and max methods of Math class, min returns minimum of two integers and max returns maximum of two integers. Following will produce an error:
min();
We need to write class name because many classes may have a method with same name which we are calling.

Using multiple classes in Java program

Java program can contain more than one i.e. multiple classes. Following example Java program contain two classes: Computer and Laptop. Both classes have their own constructors and a method. In main method we create object of two classes and call their methods.

Using two classes in Java program

class Computer {
  Computer() {
    System.out.println("Constructor of Computer class.");
  }
 
  void computer_method() {
    System.out.println("Power gone! Shut down your PC soon...");
  }
 
  public static void main(String[] args) {
    Computer my = new Computer();
    Laptop your = new Laptop();
 
    my.computer_method();
    your.laptop_method();
  }
}
 
class Laptop {
  Laptop() {
    System.out.println("Constructor of Laptop class.");
  }
 
  void laptop_method() {
    System.out.println("99% Battery available.");
  }
} 
Output of program:
Multiple classes Java program
You can also create objects in method of Laptop class. When you compile above code two .class files will be created which are Computer.class and Laptop.class, this has the advantage that you can reuse your .class file somewhere in other projects without compiling the code again. In short number of .class files created will be equal to number of classes in code. You can create as many classes as you want but writing many classes in a single file is not recommended as it makes code difficult to read rather you can create single file for every class. You can also group classes in packages for easily managing your code.


Java constructor tutorial with code examples

Constructor java tutorial: Java constructors are the methods which are used to initialize objects. Constructor method has the same name as that of class, they are called or invoked when an object of class is created and can't be called explicitly. Attributes of an object may be available when creating objects if no attribute is available then default constructor is called, also some of the attributes may be known initially. It is optional to write constructor method in a class but due to their utility they are used.

Java constructor example

class Programming {
  //constructor method
  Programming() {
    System.out.println("Constructor method called.");
  }
 
  public static void main(String[] args) {
    Programming object = new Programming(); //creating object
  }
}
Output of program:
Java constructor example
This code is the simplest example of constructor, we create class Programming and create an object, constructor is called when object is created. As you can see in output "Constructor method called." is printed.

Java constructor overloading

Like other methods in java constructor can be overloaded i.e. we can create as many constructors in our class as desired. Number of constructors depends on the information about attributes of an object we have while creating objects. See constructor overloading example:
class Language {
  String name;
 
  Language() {
    System.out.println("Constructor method called.");
  }
 
  Language(String t) {
    name = t;
  }
 
  public static void main(String[] args) {
    Language cpp  = new Language();
    Language java = new Language("Java");
 
    cpp.setName("C++");
 
    java.getName();
    cpp.getName();
  }
 
  void setName(String t) {
    name = t;
  }
 
  void getName() {
    System.out.println("Language name: " + name);
  }
}
Output of program:
Java constructor overloading program
When cpp object is created default constructor is called and when java object is created constructor with argument is called, setName method is used to set 'name' attribute of language, getName method prints language name.

Java constructor chaining

Constructor chaining occurs when a class inherits another class i.e. in inheritance, as in inheritance sub class inherits the properties of super class. Both the super and sub class may have constructor methods, when an object of sub class is created it's constructor is invoked it initializes sub class attributes, now super class constructor needs to be invoked, to achieve this java provides a super keyword through which we can pass arguments to super class constructor. For more understanding see constructor chaining example:
class GrandParent {
  int a;
 
  GrandParent(int a) {
    this.a = a;
  }
}
 
class Parent extends GrandParent {
  int b;
 
  Parent(int a, int b) {
    super(a);
    this.b = b;
  }
 
  void show() {
    System.out.println("GrandParent's a = " + a);
    System.out.println("Parent's b      = " + b);
  }
}
 
class Child {
  public static void main(String[] args) {
    Parent object = new Parent(8, 9);
    object.show();
  }
}
Output of program:
Java constructor chaining program example
Constructor method doesn't specify a return type, they return instance of class itself.


No comments:

Post a Comment

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...