Hello guys, welcome to lingarajtechhub.com . Today we are going to discuss the display the properties of Tv using Outer class and Inner class.
Code:
package com.Tv.java; public class Tv { private String company="LG"; class Details{ float inch=20.5f; } public static void main(String[] args) { Tv t=new Tv(); Details d=t.new Details(); System.out.println(t.company); System.out.println(d.inch); } }
Line-1
- package com.Tv.java
It is a java package , this package is created in a java project and it contains classes & interfaces inside the package.
Line-2
- public class Tv
Tv 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
- private String company=”LG”
Here company is a variable.String is a class. It is not a data type.Here assign company value.It is private means we can’t access that value outside of the class.
Line-5
- class Details
Details are the class name. class is a template used to create objects. Inside the outer class we create the inner class.
Line-6
- float inch=20.5f
Here inch is a variable. float is a data type of the variable. Here assign inch value.
Line-8
- 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.
- Tv t=new Tv()
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.
- Details d=t.new Details()
Here “d” is the instance variable.It is created to assign the default values to the instance variables of the class when an object is created.
- System.out.println(t.company);
System.out.println(d.inch);
system is a predefined class .Out is a static variable of type printstream present in system class. Println shows the result.