SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Static Members
 The ‘static’ keyword in java is mainly
used for memory management.
 ‘static’ is applied to variables, methods,
blocks and nested classes.
 The variables and methods declared
in the class are referred as instance
members because a new copy of each
of them is created for each object.
 A member that is common to all the
objects and accessed without using a
particular object, those are declared
using static keyword.
Example:
 static int count;
 static int max(int x, int y);
 The members that are declared as
static are referred as static members,
and the static variables, static methods
are also referred as class variables,
and class methods.
‘static’ variables
 If you declare any variable with ‘static’, it is
known as ‘static variable’.
 The ‘static’ variable is used to refer common
properties of all objects(not unique for each
object).
 Ex: Company name of all employees,
College name for all the students.
 It gets memory only once in class area at the
time of class loading.
‘static’ methods
 ‘static’ keyword is used with any
method, is called ‘static method’.
 ‘static method’ belongs to the class
rather than object of a class.
 Methods declared as static have several
restrictions.
 1. They can only call other static methods.
 2. They must only access static data.
 3. They cannot refer to this or super in
any way.
 4. We can access the static member by
using the class name itself.
 Example 1:
//Defining and using static members
class MathOperation
{
static float mul(float x,float y)
{
return x*y;
}
static float divide(float x,float y)
{
return x/y;
}
}
class MathApplication
{
public static void main(String args[])
{
float a=MathOperation.mul(4.0,5.0);
float b=MathOperation.divide(a,2.0);
System.out.println("b="+b);
}
}
Example 2:
class Student
{
int rollno;
String name;
static String college=“VLITS”;
static void change() {
college=“Vignan University”;
}
Student(int r,int n) {
rollno=r;
name=n;
}
void display() {
System.out.println(roll no+” ”+name+” ”+college);
}
}
class StudentsInfomethod
{
public static void main(String args[])
{
Student.change()
Student s1=new Student(501,“Pavan”);
Student s2=new Student(502,“Kalyani”);
Student s3=new Student(503,“Vignesh”);
s1.display();
s2.display();
s3.display();
}
}
Static Block
 A static block is a block of statements declared as
static, something like this:
static
{
Statements;
}
 JVM executes a static block on a highest priority
bases. This means JVM first goes to static block
even before it looks for the main() method in the
program.
Example:
class Test
{
static
{
System.out.println("static block");
}
public static void main(String args[])
{
System.out.println("main block");
}
}
Output:
Static block
main block
Static class
 When we declare a certain member as
static, we do not have to create an
instance of the class to access it.
 Syntax
class Outer
{
static class Nested
{
}
}
 Here we have declared an Outer class
and then declared a nested class.
 Static classes in Java can be created
only as nested classes.
 Inner class- Inner classes are the
classes that are non-static and nested.
 They are written inside an outer class.
 We are unable to create an instance of
the inner class without creating an
instance of its given outer class.
 This is needed when the user has to
create a class but doesn't want other
classes to access it. Here we can use
an inner class for that reason.
Outer class- Outer classes are the
classes in which nested or inner classes
are defined.
Nested class- Nested class can be static
or non-static. The non-static nested
class is known as an inner class.
An instance of a static nested class
can be created without the instance of
the outer class. The static member of the
outer class can be accessed only by the
‘static’ class
 Static class in Java is a nested class and it
doesn't need the reference of the outer
class.
 Static class can access only the static
members of its outer class, cannot access
the non-static members.
 Inner classes can access the static and
the non-static members of the outer class.
class Outer {
// static member of the outer class
private static char grade = 'A';
// Static class
static class Nested {
//non-static method
public void fun() {
// nested class can access the static
members // of the outer class
System.out.println("Grade: " + grade);
}
}
public static void main(String args[]) {
Outer.Nested obj = new Outer.Nested();
//creating an object of nested
// class without creating an object
// of the outer class.
obj.fun();
}
}
Example 2:
class TestOuter1{
static int data=30;
static class Inner{
void msg()
{
System.out.println("data is "+data);}
}
public static void main(String args[])
{
TestOuter1.Inner obj=new TestOuter1.Inner
();
obj.msg();
}
}
Thank you

Weitere ähnliche Inhalte

Ähnlich wie Static Members-Java.pptx

9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesSakkaravarthiS1
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorialrajkamaltibacademy
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in javaRicha Singh
 
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptxParvizMirzayev2
 
Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVATech_MX
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfbca23189c
 
this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdfParvizMirzayev2
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programmingshinyduela
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword pptVinod Kumar
 
A1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.pptA1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.pptRithwikRanjan
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 

Ähnlich wie Static Members-Java.pptx (20)

9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
 
BCA Class and Object (3).pptx
BCA Class and Object (3).pptxBCA Class and Object (3).pptx
BCA Class and Object (3).pptx
 
this keyword in Java.pptx
this keyword in Java.pptxthis keyword in Java.pptx
this keyword in Java.pptx
 
Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVA
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
 
this keyword in Java.pdf
this keyword in Java.pdfthis keyword in Java.pdf
this keyword in Java.pdf
 
Nested class
Nested classNested class
Nested class
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Chap11
Chap11Chap11
Chap11
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
A1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.pptA1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.ppt
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 

Kürzlich hochgeladen

Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 

Kürzlich hochgeladen (20)

Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 

Static Members-Java.pptx

  • 2.  The ‘static’ keyword in java is mainly used for memory management.  ‘static’ is applied to variables, methods, blocks and nested classes.
  • 3.  The variables and methods declared in the class are referred as instance members because a new copy of each of them is created for each object.  A member that is common to all the objects and accessed without using a particular object, those are declared using static keyword.
  • 4. Example:  static int count;  static int max(int x, int y);  The members that are declared as static are referred as static members, and the static variables, static methods are also referred as class variables, and class methods.
  • 5. ‘static’ variables  If you declare any variable with ‘static’, it is known as ‘static variable’.  The ‘static’ variable is used to refer common properties of all objects(not unique for each object).  Ex: Company name of all employees, College name for all the students.  It gets memory only once in class area at the time of class loading.
  • 6. ‘static’ methods  ‘static’ keyword is used with any method, is called ‘static method’.  ‘static method’ belongs to the class rather than object of a class.
  • 7.  Methods declared as static have several restrictions.  1. They can only call other static methods.  2. They must only access static data.  3. They cannot refer to this or super in any way.  4. We can access the static member by using the class name itself.
  • 8.  Example 1: //Defining and using static members class MathOperation { static float mul(float x,float y) { return x*y; } static float divide(float x,float y) { return x/y; } }
  • 9. class MathApplication { public static void main(String args[]) { float a=MathOperation.mul(4.0,5.0); float b=MathOperation.divide(a,2.0); System.out.println("b="+b); } }
  • 10. Example 2: class Student { int rollno; String name; static String college=“VLITS”; static void change() { college=“Vignan University”; } Student(int r,int n) { rollno=r; name=n; } void display() { System.out.println(roll no+” ”+name+” ”+college); } }
  • 11. class StudentsInfomethod { public static void main(String args[]) { Student.change() Student s1=new Student(501,“Pavan”); Student s2=new Student(502,“Kalyani”); Student s3=new Student(503,“Vignesh”); s1.display(); s2.display(); s3.display(); } }
  • 12. Static Block  A static block is a block of statements declared as static, something like this: static { Statements; }  JVM executes a static block on a highest priority bases. This means JVM first goes to static block even before it looks for the main() method in the program.
  • 13. Example: class Test { static { System.out.println("static block"); } public static void main(String args[]) { System.out.println("main block"); } } Output: Static block main block
  • 14. Static class  When we declare a certain member as static, we do not have to create an instance of the class to access it.  Syntax class Outer { static class Nested { } }  Here we have declared an Outer class and then declared a nested class.
  • 15.  Static classes in Java can be created only as nested classes.  Inner class- Inner classes are the classes that are non-static and nested.  They are written inside an outer class.  We are unable to create an instance of the inner class without creating an instance of its given outer class.  This is needed when the user has to create a class but doesn't want other classes to access it. Here we can use an inner class for that reason.
  • 16. Outer class- Outer classes are the classes in which nested or inner classes are defined. Nested class- Nested class can be static or non-static. The non-static nested class is known as an inner class. An instance of a static nested class can be created without the instance of the outer class. The static member of the outer class can be accessed only by the
  • 17. ‘static’ class  Static class in Java is a nested class and it doesn't need the reference of the outer class.  Static class can access only the static members of its outer class, cannot access the non-static members.  Inner classes can access the static and the non-static members of the outer class.
  • 18.
  • 19. class Outer { // static member of the outer class private static char grade = 'A'; // Static class static class Nested { //non-static method public void fun() { // nested class can access the static members // of the outer class System.out.println("Grade: " + grade); } }
  • 20. public static void main(String args[]) { Outer.Nested obj = new Outer.Nested(); //creating an object of nested // class without creating an object // of the outer class. obj.fun(); } }
  • 21. Example 2: class TestOuter1{ static int data=30; static class Inner{ void msg() { System.out.println("data is "+data);} } public static void main(String args[]) { TestOuter1.Inner obj=new TestOuter1.Inner (); obj.msg(); } }