• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Inner classes..garbage collection mumbo jumbo

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys
Pl go thru the code below that I have written It's crazy..I accept.But I started to try out few things and evolved with more questions.As you might have guessed,the questions are at the end.
public class Test{
static int num=0;
public Test(){
System.out.println(num+"--from outer constructor");
}
public static void main(String args[]){
String s[]={"from"," outer"};
System.out.println("HI");
Inner i=new Inner();
i.main(s);
num+=1;
System.out.println(num+"--from outer main");
}
public static void getOut(){
System.out.println("Good Bye");
System.exit(0);
}
public static class Inner{
public Inner(){
System.out.println(num+"--from inner constructor");
}
public static void main(String args[]){
String s1[]={"from"," inner"};
System.out.println("HI from inner");
Test t=new Test();
num+=1;
System.out.println(num+"--from inner main");
if (num>02)
t.getOut();
t.main(s1);
}
}
}
Following are the questions.
1.In continuation of my experiments with static inner class,How can a static instantiate with constructor??How does it behave--accessig its methods etc..and its use as static.
2.Calling main() method.Is it valid everywhere??.I really lost here.Two main,both static excecuting as normal methods.
3.With objects being created and re-created to the same reference for both outer and inner,which are all the eligible candidates for garbage collection and How may objects get collected when exited??
Let me stop here to contain your patience.
PS:Paul, you see here the instansiation static inner class here as you said.
Thanx
 
Trailboss
Posts: 23990
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) A static inner class just means that the class won't use non-static attributes of the outer class.
2) Are you able to activate the inner main() directly from the command line?
3) I don't understand the question.
I see the inner class instantiation. Looks tidy.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul
I was asking about Garbage collection and the no of objects ready to be collected when exited??
 
paul wheaton
Trailboss
Posts: 23990
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, are you making an infinite loop?
It looks like you call inner.main() which calls Test.main() which calls inner.main().
Have you tried to run this program?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Pual. I compiled and ran this pgm.Its perfect.As you see in the code I am forcing to exit by calling the System method after if reaches two iterations.My intention is to create new objects and overwrite them again with the same reference and know whether the objects created were GC'ed when they lose references.
Also as you see in the code I was calling main explicitly and trying few things with inner classes.
I also tried to include an inner class(with normal methods) in an INTERFACE.NO problem.This raised lot more questions about inner classes.How do I access the methods in the inner class etc..anything thing in detail about the inner clas behaviour will be helpful.Any noble souls out there to clear me??
Thanx
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BujjiRaj- an inner class in an interface is actually a static inner class, even if you don't provide the word "static" yourself - it's provided implicitly, as is a "public" modifier. So you can access the class and its methods the same way you did for your static inner class "Inner".
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic