Hello guys, welcome to lingarajtechhub.com. Today we are going to discuss the display properties of Tiger using class & object in the oops concept.
Code:
package com.tiger_oops_class_obj.java; public class Tiger { String colour,gender; void Tiger_Input() { colour="white"; gender="female"; } void Tiger_Display() { System.out.println("Tiger colour:"+colour); System.out.println("Tiger gender:"+gender); } public static void main(String[] args) { Tiger t=new Tiger(); t.Tiger_Input(); t.Tiger_Display(); } }
Line-1:
- package com.tiger_oops_class_obj.java;
It is a java package , this package is created in a java project and it contains classes inside the package.
Line-2:
- public class Tiger
“Tiger” 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-3:
- String colour,gender
Here the name is the variable and String is a class. It is not a data type.
Line-5:
- void Tiger_Input() {
colour=”white”;
gender=”female”;
}
In this line we create a method whose name takes Tiger_input() where store properties values.
Line-9:
- void Tiger_Display() {
System.out.println(“Tiger colour:”+colour);
System.out.println(“Tiger gender:”+gender);
}
In this line we create a method whose name takes Tiger_display().system is a predefined class .Out is a static variable of type printstream present in system class. Println shows the result.
Line-14:
- 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-15:
- Tiger t=new Tiger();
Here “t” 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-16:
- t.Tiger_Input()
“t” is the instance variable which is created. “ .” is the connector .Tiger_input() is a method where our input values will be stored.
Line-17:
- t.Tiger_Display()
“t” is the instance variable which is created. “ .” is the connector . Tiger_display() is a method where our result will be stored.