Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can we declare a method as transient?

 
viki Bhardwaj
Ranch Hand
Posts: 74
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2952
59
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3036
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2952
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek Bhardwaj wrote:so is it mistake of decompiler ??

Yes, the decompiler clearly doesn't understand varargs.
 
Paul Clapham
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3036
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic