Class, Object and Constructor
1. Class.
Class - is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.
2. Block - is a set of code enclosed within curly braces {} within any class , method, or constructor.
There are two types of block in Java : static Block, instance (non-static) Block.
3. Constructor.
Constructor - is a similar to method that invoked when an object of the class is created. Konstruktorun return tipi olmur. Eger biz ozumuz konstruktor yaradiriqsa o zaman Java ozu default konstruktor yaratmir. Konstuktor hemiwe instance block-dan sonra iwe duwur.
Exercise 1:
public class Main {
static {
System.out.println("This is static block");
Main main = new Main();
}
{
System.out.println("This is instance block");
}
public Main() {
System.out.println("This is a constructor");
}
public static void main(String[] args) {
System.out.println("This is a main method");
Output:
This is static block
This is instance block
This is a constructor
This is a main method
Exercise 2:
public class Main {
static {
System.out.println("This is static block");
Main main = new Main();
}
{
System.out.println("This is instance block");
}
public Main() {
System.out.println("This is a constructor");
}
public static void main(String[] args) {
System.out.println("This is a main method");
Main main = new Main();Output:
This is static block This is instance block This is a constructor This is a main method This is instance block This is a constructor
Комментарии
Отправить комментарий