Hello guys, welcome to lingarajtechhub.com . Today we are going to discuss display properties of Mobile using class & object in the oops concept.
Code:
package com.Mobile_oops_class_obj.java; import java.util.Scanner; public class Mobile { String Model, colour; void Mobile_input() { System.out.println("Enter value of model & colour:"); Scanner sc=new Scanner(System.in); Model=sc.next(); Colour=sc.next(); } void Mobile_Display() { System.out.println("Mobile model:"+Model); System.out.println("Mobile Colour:"+colour); } public static void main(String[] args) { Mobile m=new mobile(); m.Mobile_input(); m.Mobile_Display(); } }
Line-1:
- package com.Mobile_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-3:
- import java.util.Scanner;
In java, Scanner is a class. util package used for obtaining the input of the data types like int, double, float , char and strings.
Line-5:
- public class Mobile
“Mobile” 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-6:
- String Model,colour
Here Model , colour are the variable and String is a class. It is not a data type.
Line-8:
- void Mobile_input() {
System.out.println(“Enter value of Model & colour :”)
In this line we create a method whose name takes Mobile_input().system is a predefined class .Out is a static variable of type printstream present in system class. Println shows the result.
Line-10:
- Scanner sc=new Scanner(System.in)
Scanner is a class and sc is the instance of the object which is the new Scanner . System.in is an argument.
Line-11:
- Model =sc.next();
- colour=sc.next();
Model , colour are the variables that we take and sc is our instance name which is created for an object and next() is a method of a Scanner object that reads in a string of digits or char and converts them into an String type.
Line-14:
- void Mobile_Display() {
System.out.println(“Mobile model:”+Model);
System.out.println(“Mobile colour:”+colour);
}
In this line we create a method whose name takes Mobile_display().system is a predefined class .Out is a static variable of type printstream present in system class. Println shows the result.
Line-19:
- 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-20:
- Mobile m=new Mobile()
Here “m” is the instance variable.It is created to assign the default values to the instance variables of the class when an object is created.
- m.Mobile_input()
“m” is the instance variable which is created. “ .” is the connector Mobile_input() is a method where our input values will be stored.
- m.Mobile_Display();
“m” is the instance variable which is created. “ .” is the connector .Mobile_display() is a method where our result will be stored.