• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Accessing the object of Local Class  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,
This is jason, i have small problem, i want your help.
class Test
{
void show()
{
class Local
{
void display()
{
System.out.println("display method");
}
}
}
public static void main(String args[])
{
// How to create the object of Local class here
}
}
Thanks
Bye

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Naaaaah , no way. You cant do that.
 
jason picardo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sahir Shah:
if you can please explain me why?
Bye
Thank's
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can create an instance of local class only within the method inside which it is defined. so if your function
show()
was returning an object type, then it could perhaps create the instance and return it.
So where ever you may call the function from, you can access the object.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by jason picardo:

Hi,
This is jason, i have small problem, i want your help.
class Test
{
void show()
{
class Local
{
void display()
{
System.out.println("display method");
}
}
}
public static void main(String args[])
{
// How to create the object of Local class here
}
}
Thanks
Bye


Originally posted by Sahir Shah:

Naaaaah , no way. You cant do that.


Originally posted by jason picardo:
hi Sahir Shah:
if you can please explain me why?
Bye
Thank's


Jason Picardo ,
Yes , The Local class objects can be created outside not only that method even outside that class also. the code given below shows you how it is possible.this code contain two class files Global & GetReady . there is a method in Global class named create . inside that create method there is a Local class named Local so my ultimate goal is to create that Local class object outside that class Global. for that i define a method named create which returns a Runnable Object which is actully Local Object.
In the next Class GetReady I create an Object of class Global. and after that i create a Runnable object with the help of create method. so the Object return by that create method is actully Local class Object. to show this I use some methods
to extract the original name of that object. the first line printed by the GetReady Class is the name of That Local class. so Next is to call any method on that available in the Local class . actully directly we can call only run() method of
that Local Class because Compile Time Type of that object is Runnable not Local. so for calling another method available in the local class named localMethod() i make use of Java's Reflection Mechanism which is used in one of the very advanced
feature of Java and you may see that process can call LocalMethod() defined In the Local class explicitly. and also use local variable s to print the output. the output can be changed to whatever (i.e. by using diff parameter in the constructor of local class)
CODE------>
separate file Global.java
<pre>
class Global {
Object create() {
class Local {
String s;
Local(String s) {this.s=s;}
public void run(){}
public String localMethod() {System.out.println("yes it executes\n");
System.out.println(" it can access local variable defined inside Local and value of local s is "+s);
System.out.println("it can use any constructor to create itself again & again");
System.out.println("yes it executes every method not only overriding interface's run () methods but all");
System.out.println("even you can pass parameter to constructor & methods at run time");
return "Local Method Execution completes";
}}
return new Local("put here what you want to test");
}
}
</pre>
separate file GetReady.java
<pre>
import java.lang.reflect.*;
class GetReady {
public static void main(String arg[]) throws Exception {
Global g = new Global();
Object r = g.create();
System.out.println(r.getClass().getName());
//above line displaying Local class name for which that object r //belongs.
Method []m = r.getClass().getMethods();
//this returns methods in superclass / alphabetic order.
System.out.println(m[9].invoke(r,null));
//used here m[9] because the upper 9 methods
// from (0 to 8) is from Object class
}
} </pre>
even we are not bounded to create local class object in this way . we can create that object direct with the use of new operator. but for that we have to write our own ClassLoader which loads into memory the necessary resources.
One More thing Jason Java isnot made by a person who has four hands or four legs or he is having horn. instead this is made by peoples like you and me. this is the true that in normal circumstances it is not needed to instantiate local class . so this feature is not provided in the java semantics. but the Local class are also equilant as top level classes and instantiated in same way although it is not required or not recommended by java's makers. so it is not available in a handy way.but by capturing the class instantiation mechanism it can be easily done by writing our custom classloader. i am not jocking i have my own classloader with the help of which i can load local classes with new operator although it's meaningless.
My search is still in progress ............
now what prasad ?
[This message has been edited by Govinda (edited November 26, 2000).]
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Govindaaa Gooovinda
Telling simple thing in more complex way.Dear friend you can't get local classes defined inside method without returning the instance of the class ;as you did
return new Local("put here what you want to test");
U are returning third party reference to refer local class i.e through Runnable interface.

HTH
Prasad

------------------
 
Govinda
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
offcourse , it's a ordinary way to full fill our task . but without applying this way calling localMethod inside Local is become difficult from an outside instance. that Runnable reference alone is not capable of calling or referencing any feature not provided in Runnable interface (except run() method). try . now i am changing that code again hava a look Please.

Originally posted by Prasad Ballari:
Govindaaa Gooovinda
Telling simple thing in more complex way.Dear friend you can't get local classes defined inside method without returning the instance of the class ;as you did
[b]return new Local("put here what you want to test");

U are returning third party reference to refer local class i.e through Runnable interface.

HTH
Prasad

[/B]



[This message has been edited by Govinda (edited November 26, 2000).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!