class Class1{ private void met1() { } } class Class2{ public static void main(String[] args) { } } class MainClass{ } int x;// public ================================= Constructors: ============= ==> special method in class define name with class name only for defining the data of class data members. Syntax: ClassName() { definitions; } Note: ===== The Constructor con be invoked automatically at the time of object creation. ==> Constructor can be defined in two ways: 1) Constructor without parameters/Parameter-less Constructor ============================================================= Syntax: ClassName() { definitions; } ==> the above definition can share the same data among different objects. So, here the JVM can store different objects with same data in different heap memory locations. Because of this, the wastage of memory can take place. 2) Constructor with parameters/Parameterized Constructor ========================================================= Syntax: ClassName(type par1, type par2, type par3,...) { definitions } package AshokIT.OOPs; class ClassStudent{ private int stuid; private String stuname; private char gender; // this constructor can share the same data among multiple objects. // ClassStudent(){ // stuid = 101; // stuname = "Karthik"; // gender = 'M'; // } ClassStudent(int id,String name,char gen) { stuid = id; stuname = name; gender = gen; } void display() { System.out.println("The Student Data = "); System.out.println("Student Id = "+stuid); System.out.println("Student Name = "+stuname); System.out.println("Student's Gender = "+gender); } } public class Constructors { public static void main(String[] args) { ClassStudent stu1 = new ClassStudent(101,"Keerthi",'F'); stu1.display(); ClassStudent stu2 = new ClassStudent(201,"Kumar",'M'); stu2.display(); } } ========================================= Parameters/Arguments: ===================== ==> use to carry the data to methods. met1(int a, int b) // formal arguments { return a+b; } met1(10,20); // actual arguments ==> Arguments are of two types: 1) Actual Arguments 2) Formal Arguments ======================================== If the parameters of the constructor and data members in class are with same name: ================================================================================== package AshokIT.OOPs; class MyClass1{ int a; float b; char c; double d; MyClass1(int a,float b,char c,double d) { // parameters ==> local variables this.a = a; // assigning the local variable value to the class data member this.b = b; this.c = c; this.d = d; } void display() { System.out.println("a = "+a+"b = "+b+"c = "+c+"d = "+d); } } public class MainClassImplementation { public static void main(String[] args) { MyClass1 mc = new MyClass1(100,1.23f,'b',123.234); mc.display(); } }