Hello guys, welcome to lingarajtechhub.com . Today we are going to discuss the display details of customers through default constructor.
Code:
package com.Customer_default.java; public class Customer { String name,address; int DOB,salary; Customer(){ name="Gyana ranjan Sahoo."; address="BBSR"; DOB=22/05/1994; salary=25000; } void show() { System.out.println("Customer details are:"); System.out.println("Name:"+name); System.out.println("Address:"+address); System.out.println("DOB:"+DOB); System.out.println("Salary:"+salary); } public static void main(String[] args) { Customer A=new Customer(); A.show(); } }
Line-1:
- package com.Customer_default.java;
It is a java package , this package is created in a java project and it contains classes inside the package.
Line-3:
- public class Customer
“Customer” is the class-name , class is a template used to create objects & class-name first letters must be capital. It is public means we can use this outside of the package.
Line-4:
- String name,address;
Here name,address are the variables and String is a class. It is not a data type.
Line-5:
- int DOB,salary
DOB,salary are the variables and int is a data type of the variable.
Line-7:
- Customer(){
name=”Gyana ranjan Sahoo.”;
address=”BBSR”;
DOB=22/05/1994;
salary=25000;
}
“Customer” is a constructor which is user defined.Constructor name is always same as class- name .Inside the constructor we assign value .
Line-14:
- void show() {
System.out.println(“Customer details are:”);
System.out.println(“Name:”+name);
System.out.println(“Address:”+address);
System.out.println(“DOB:”+DOB);
System.out.println(“Salary:”+salary);
}
In this line we create a method whose name takes show().system is a predefined class .Out is a static variable of type printstream present in system class. Println shows the result.
Line-22:
- public static void main(String[] args)
It is a main method in which we can execute a program.
- public : It is an access modifier. The main() is public because we can access main() outside of the class , if main() can’t be public then JVM(Java Virtual Machine) can’t be called main() & code can’t be executed.
- Static : static is a keyword in which the main method has to be static in which JVM can load the class into memory and call the main method without creating an instance.
- Void : Void is a data type. It means no return type.
- main(): Main is a function.
- String [] args : string means collection of characters ,String[] args represents a collection of Strings that are separated by a space and can be typed into the program on the terminal directly.
Line-23:
- Customer A=new Customer();
Here “A” is the instance variable.It is created to assign the default values to the instance variables of the class when an object is created.
Line-24:
- A.show();
“A” is the instance variable which is created. “ .” is the connector .show() is a method where our result will be stored.