wood burning stoves 2.0*
The moose likes Java in General and the fly likes can we declare a method as transient? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "can we declare a method as transient?" Watch "can we declare a method as transient?" New topic
Author

can we declare a method as transient?

viki Bhardwaj
Ranch Hand

Joined: Nov 07, 2011
Posts: 74

Hi Ranchers!
I have a doubt regarding uses of transient modifier ,let me explore my doubt . As i know, transient modifier only can be used in the case of variable , i got surprise when i found that jvm internally append transient modifier in main method only in the case of an abstract class which is going to inherited by its child class....I would like to show the scenario first, please have a look at below code....

abstract class AbstractDemo{

}
class SubAbstractDemo extends AbstractDemo{
public static void main(String...args){

}
}

when i compiled above code and decompiled child class SubAbstractDemo ,i really got surprise when i saw the following below code......

class SubAbstractDemo extends AbstractDemo
{

SubAbstractDemo()
{
}

public static transient void main(String args[])
{
}
}


here jvm insert transient modifier internally but when i did it explicitly then i faced compile time error saying modifier transient not allowed here.
how it happens?? m confused??? and what is mean here to use transient in the case of main() method...
please give me reason why ?? and how??
thanks for your valuable time.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2249
    
  47
You can't declare a method as transient.

I think you may find the main method is not marked by the jvm internally as transient but rather as a varargs method, the decompiler is possibly getting confused.
Try declaring a method with a varargs parameter, compile and decompile the class and see if you get the same issue.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
I agree with Tony that it's probably a varargs method. In the JVM specification on the class file format, Table 4.19 shows that a field modifier for "transient" is represented with the value 0x0080. This happens to be the same as the value used for a method modifer for varargs, from Table 4.20. It's likely that the decompiler mixed these up, as varargs didn't exist as a method modifier in earlier versions of the language. Vivek, note that your decompiler may have been written a long time ago, and may not have all modern features. What decompiler are you using? And when was it last updated? I haven't heard much about decompilers in years, and I don't know if there are any that are fully current with the latest language features.
viki Bhardwaj
Ranch Hand

Joined: Nov 07, 2011
Posts: 74

Tony Docherty wrote:You can't declare a method as transient.

Try declaring a method with a varargs parameter, compile and decompile the class and see if you get the same issue.


yeah! Mr Tony, you are absolutely right, when i did same thing with varargs method , i observed same thing as you describe...
class Test{
public void check(String...args){
}
}

after decompilation....code look like
class Test{
Test(){}
public transient void check(String [] args){}
}
viki Bhardwaj
Ranch Hand

Joined: Nov 07, 2011
Posts: 74

Mike Simmons wrote:Vivek, note that your decompiler may have been written a long time ago, and may not have all modern features. What decompiler are you using? And when was it last updated?


I am using DJ java decompiler v.3.12.12.96 and also i decompile the code from showmycode but i found same thing from there.
finally is it true that jvm won't insert transient modifier in the case of varargs method while we see it after decompilation , so is it mistake of decompiler ??
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2249
    
  47
Vivek Bhardwaj wrote:so is it mistake of decompiler ??

Yes, the decompiler clearly doesn't understand varargs.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

From what I can see, decompilers generally didn't cope well with the new features of Java 5, which included a lot of things beyond varargs.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3012
    
  10
DJ is just a GUI wrapper around JAD, which was never updated for Java 5. I'm unfamiliar with showmycode.com, but I suspect it also uses JAD internally. There's apparently a newer project, the very creatively named Java Decompiler, that may give you better results.

Vivek Bhardwaj wrote:finally is it true that jvm won't insert transient modifier in the case of varargs method

Table 4.20 that I linked to earlier shows that there is no transient modifier for methods, period. It's not something that is representable in a .class file.
viki Bhardwaj
Ranch Hand

Joined: Nov 07, 2011
Posts: 74

I'm very happy after getting the valuable answers from you Guys regarding my confusion...thank you very much.. Tony and Mike,
yeah! JD-decompiler is working nicely, better than DJ...thanks for your recommendation..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can we declare a method as transient?