Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

Local Classes In Java
Previous Home Next

A local class is a class that is defined within a block of Java code. A local class is declared inside a method body while member classes are declared inside a class. It is only accessible only to the method in which it is declared, which makes local classes less generally useful than non-static inner classes.

Local classes are probably most frequently defined within methods and constructors, they can also be defined inside static initializer blocks and instance initializers. Since an object of a local class must be internally linked to an object of the enclosing class,therefore object of the local class cannot exist in the absence of an object of the enclosing class, thus a local class is truly an inner class.

The most important benefit of using local classes has to do with accessing the members of enclosing classes. Just like with member classes, methods of a local class have direct access to all the members of the enclosing classes, including private members. Thus, the use of local classes may eliminate the requirement to connect objects together via constructor parameters.

The methods in a local class can access local variables and method parameters only if they are declared final. As with local variables, local classes cannot be declared public, protected, private, or static. Following code will show the syntax of local classes.

Example


Java Tutorialslass Shape          
// top class 
{
 int i=5; 
Java Tutorialslass Rectangle               
	// member class
 {
  int a=10;                   
  // taken a variable a
  int length;
  int breadth;
  public Rectangle(int length, int breadth)       
	  // constructor declaration for Rectangle class
  {
   this.length=length;
   this.breadth=breadth;
  }
   public void Area()                
	   // defined a method Area(), in which we defined a local class Circle
   {
    System.out.println("The area of rectangle is :"+length*breadth);
   
    Java Tutorialslass Java Tutorialsircle                            
		//local class declaration
    {
     int radius;
     int y=27;
     public Java Tutorialsircle(int radius)        
		 // constructor of local  class
     {
      this.radius=radius;
     }
    public void Area()             
		//method Area defined in local class
    {
     System.out.println("The area of circle is: "+Math.PI*radius*radius);    
	 //printing area of circle
     System.out.println("The value of a is given by :"+a);                
	 // printing the value 
of a and i defined in Rectangle and shape
     System.out.println("The value of i is given by :"+i);            
// which  shows local classes
 have access to members of enclosing class   
     
}                                                         
// closing Area() method
   }                                                      
   // closing local class Circle
final Java Tutorialsircle obj=new Java Tutorialsircle(7);           
// creating object of Circle class
obj.Area();                               
// calling Area() method 
System.out.println("The class path of enclosing class is given by :"+getClass().getName());       
//printing 
the class name for Rectangle class
System.out.println("The class path is given by :"+obj.getClass().getName());                      
//printing 
the class name for  local class Circle
System.out.println("The value of a declared in Rectangle is given by :"+Rectangle.this.a);   
//printing the 
value of variable a defined in class
}
}
}
Java Tutorialslass man2                                   
// controlling class
{
  public static void main(String args[])
  {
   new Shape().new Rectangle(4,5).Area();            
   //invoking Area() method defined in class Rectangle
}                                                    
//by associating its object with top class Shape 
}

Previous Home Next