Modifier

In this tutorial, we will learn 2 types of most important modifiers „private “, “public“ and  a simple class with overloaded methods.

Every class and variable have a modifier, and class name (if not, it will be set by default and access is allowed only from the same package)
Every class has a constructor (default: constructor without parameters). A constructor initializes different objects with Attributes.

Class 1:
public class Customer{
private int ID;
Customer(int ID) { // constructor with 1 overloaded parameter
this.ID = ID; //the  keyword „this“ is a reference .  The reference object try to access the address of the variable ID
}
}



Accessing to the first class from Main:


public static void main(String[] args){
Customer c1 = new Customer(2); // we have created   a new object with Attribute
System.out.println("c1.ID");
}

Wait a minute we have a error.Why?Because ID is private.The correct solution is defining a public getter and setter methods.

Correct solution:

public class Customer{
private int ID;
Customer(int ID) { // constructor with 1 overloaded parameter
this.ID = ID; //the  keyword „this“ is a reference .  The reference object try to access the address of the variable ID

public String getID() { // 
	return this.ID; // the keyword "this" is a reference call 
}
public void setID(int ID) { 
this.firstName = name ;

}
}

Main Class:

public static void main(String[] args){
Customer c1 = new Customer(2);
System.out.println("c1.getID()");
}