aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interesting Question. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Interesting Question." Watch "Interesting Question." New topic
Author

Interesting Question.

Rahul Singhai
Greenhorn

Joined: Mar 04, 2008
Posts: 19
While going through my notes. I came across a very good question. I havent remember from where i got it.

public class StaticBase
{
static
{
run();
}
public static void run()
{
System.out.println("Base from " + Thread.currentThread().getStackTrace()[2]);
}

public static class Derived extends StaticBase
{
static
{
run();
StaticBase.run();
Derived.run();
}
public static void run()
{
System.out.println("Derived from " + Thread.currentThread().getStackTrace()[2]);
}
}

public static final void main(String[] args)
{
new StaticBase().run();
System.out.println("Here.....!");
new Derived().run();
}
}


can you guess the output?


<br />
SCJP - 93%
Warm Regards,<br />Rollicking
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9191
    
    2

the question generates an exception on my JVM


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
i got the output on my system.
the output is:-

Base from StaticBase.run(StaticBase.java:9)
Base from StaticBase.run(StaticBase.java:9)
Here......!
Derived from StaticBase$Derived.run(StaticBase.java:22)
Base from StaticBase.run(StaticBase.java:9)
Derived from StaticBase$Derived.run(StaticBase.java:22)
Derived from StaticBase$Derived.run(StaticBase.java:22)

but how it is working?

thanks
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

It is producing fillowing output

Base from com.StaticBase.<clinit>(StaticBase.java:5)
Base from com.StaticBase.main(StaticBase.java:27)
Here.....!
Derived from com.StaticBase$Derived.<clinit>(StaticBase.java:15)
Base from com.StaticBase$Derived.<clinit>(StaticBase.java:16)
Derived from com.StaticBase$Derived.<clinit>(StaticBase.java:17)
Derived from com.StaticBase.main(StaticBase.java:29)

it is producing sys out in run method and from where that method is been called i mean line number of caller.

Thread.currentThread().getStackTrace() returns an array of StackTraceElement, StackTraceElement object is used to represent single stack frame
[ August 28, 2008: Message edited by: ramesh maredu ]

SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9191
    
    2

OK now I get the same output....I actually didn't see that it has an inner class.I was actually running the code with the name of the inner class i.e. Derived...and I was getting this error

Exception in thread "main" java.lang.NoClassDefFoundError: Base (wrong name base)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

but I don't understand how I was getting this exception???
[ August 28, 2008: Message edited by: Ankit Garg ]
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

try this


see the API of StackTraceElement it says answer

"An element in a stack trace, as returned by Throwable.getStackTrace(). Each element represents a single stack frame. All stack frames except for the one at the top of the stack represent a method invocation. The frame at the top of the stack represents the execution point at which the stack trace was generated. Typically, this is the point at which the throwable corresponding to the stack trace was created."
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Interesting Question.
 
Similar Threads
Overriding
start and run method
Problem on Polymorphism
Who called?
Static methods & Overriding!