Submit Your Site To The Web's Top 50 Search Engines for Free! Java Tutorial: Learn Java Basics For Free | Learn Java

Tuesday, June 13, 2023

A Basic Java Program For Beginners 2023

 A Basic Java Program For Beginners 2023

                 Static Nested Class


1) Static Nested ClassIn java static nested class is a class which is defined in a class with static keyword.

static nested class cannot access non-static data member and methods of outer class. It can access only static data member of outer class including private.

 Java Static Nested Class Example

class Simple
{
static int a = 700;
static class SimpleInner
{
void show()
{
System.out.println("value is"+a);
}
}
public static void main(String args[])
{
Simple.SimpleInner s = new Simple.SimpleInner();
s.show();
}
}

https://cjavapoint.blogspot.com/?m=1
Blogspot
Core Java Interview Questions - Learn Java
Java Tutorial or Learn Java or Core Java Tutorial or Java Programming Tutorials for beginners and professionals with core concepts and examples

Non-Static Nested Class(inner class)


*1) Member Inner Class* 
 
In java member inner class is a class which is defined in class but outside of a method is called member inner class.

*Java Member Inner Class Example* 

Java member inner class defined in a class but outside of method of that class.

In this example we will create outer class(MemberOuter) and inner class(MemberInner) and declare a private data member in outer class and access this private data member in inner class because inner class can access all the data members and methods in inner class, including private data member and methods.

class MemberOuter
{
private int salary = 4000;//private data member of outer class
class MemberInner
{
void get()
{
System.out.println("salary is "+salary);//access in inner class
}
}
public static void main(String args[])
{
MemberOuter mo = new MemberOuter();
MemberOuter.MemberInner mi = mo.new MemberInner();
mi.get();
}

output : salary is 4000 

Note :

(1)  In case of java outer and inner class, Here the java compiler creates two .class files the first is MemberOuter.class and MemberOuter$MemberInner.class.


(2) If you want to instantiate inner class, you must have to create the instance of outer class and instance of inner class is created inside the instance of outer class.

*2) Anonymous Inner Class* 

In java anonymous inner class is a class that have no name is called anonymous inner class. This class can be instantiated only once. It is usually declared inside a method or block.


Anonymous inner class can be created by two ways in java 

Using class(abstract or concrete)
Using interface


*Java Anonymous Inner Class Example* 

 abstract class AIExample
{
abstract void show();
}
class Test
{
public static void main(String args[])
{
AIExample a = new AIExample()
{
void show()
{
System.out.println("Method of anonymous class");
}
};
a.show();
}
}


output : Method of anonymous class 
 
The above example is performing anonymous by using abstract class.

*Java Anonymous Inner Class Example - Using Interface* 


This is simple example of anonymous inner class using interface.
 
interface AnonymousExmple
{
void show();
}
 
class Test
{
public static void main(String args[])
{
AnonymousExmple ae = new AnonymousExmple()
{
public void show()
{
System.out.println("hello java ");
}
};
ae.show();
}
}
output : hello java

*3) Java Local Inner Class* 

In java local inner class is a class which is created inside a method is called local inner class in java. If you want to invokes the method of local inner class, you must instantiate this class inside the method.


*Java Local Inner Class Example* 

class LICExample
{
private int a = 40;
void show()
{
class LICExample1
{
void get()
{
System.out.println("value is "+a);
}
}
LICExample1 l = new LICExample1();
l.get();
}
public static void main(String args[])
{
LICExample ll = new LICExample();
ll.show();
}
}

output : value is 40

No comments:

Post a Comment

Java Inheritance Update 2023 With Program And Example

  Java Inheritance Update 2023 With Program And Example  nheritance is one of the essential concepts in object-orientated programming  (OOP)...