• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Local Class Doubt

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As I knew the scope of the Local Class is in the block/Method in which it is declared.
just tried this example
the statement Local.Innerlocal il=l.new Innerlocal(); throwing error.
is there any way to access local class in main?

Thanks
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Madhu



void loc()
{
final int a=10;
class Innerlocal
{
public void inner()
{
System.out.println("Inner world"+a);
}
}

Innerlocal i=new Innerlocal();
i.inner();

}


You have to create the object of the method or block local inner class with in that scope only.
you cannot create the object of that ,out side of that scope.

Check the bold one above

Thanks
Anil Kumar
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As I knew the scope of the Local Class is in the block/Method in which it is declared.
just tried this example
the statement Local.Innerlocal il=l.new Innerlocal(); throwing error.
is there any way to access local class in main?



As you just mentioned, a local class can't be directly "accessed" outside of the block/method that it is declared. So hence, it is out of scope when you tried to instantiate it, in the main method.

You can, however, call the loc() method from the main method -- where a local inner class object can be created and returned. There is one caveat. Since the definition of the inner class is out of scope, you can't return it as the inner class type.

Now, if this inner class, inherits from a class that is in scope, or implements an interface that is in scope, it can be referred to by that type. Unfortunately, in your case, it can only be returned to the main method as an java.lang.Object object.

Henry
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to both of you.
so my understanding is right we can access innerclass within the method loc().
Henry, sorry I couldn't get what do you mean by below statement.
your explaination with an example will be appreciated,
Thanks in advance

Originally posted by Henry Wong:

Now, if this inner class, inherits from a class that is in scope, or implements an interface that is in scope, it can be referred to by that type. Unfortunately, in your case, it can only be returned to the main method as an java.lang.Object object.

Henry

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A local class(declered in a method) is like a local variable, and is accessible just within the method only.
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by madhu v pe:
Thanks to both of you.
so my understanding is right we can access innerclass within the method loc().
Henry, sorry I couldn't get what do you mean by below statement.
your explaination with an example will be appreciated,
Thanks in advance



Basically, an object that has been created when the class definition is in scope, may still exist when the inner class definition is not in scope. If that happens, it can only be accessed by a class or interface definition that is still in scope.

For example...



In this case, the InnerLocal object that was created by the loc() method still exists in main(), but since the class definition is not in scope, it uses it's base class instead.

Henry
 
I don't even know how to spell CIA. But this tiny ad does:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic