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

Mughal Q4.26

 
Prosenjit Banerjee
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In Khalid Mughal's book question no. 4.26 on the page 130 goes like this:
Which of these are not legal declarations within a class?
Select all valid answers.
(a) static int a ;
(b) final Object[] fudge = { null } ;
(c) abstract int t ;
(d) native void sneeze() ;
(e) final transient static private double PI = 3.14159265358979323846 ;

The correct answer given in the book is c.
c is illegal, alright. But isn't e also illegal?
In that book itself it mensioned that,
Note that the transient modifier cannot be specified for static variables, as do not belong
to objects.
Here, PI is a static variable then how can it be transient?
 
Dominic Paquette
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here is my opinion:
Variables declared as transient don't get Serialized.
I just tried an example of declaring a transient, static variable and it compiled fine.
I also tried another example where i serialized(wrote to a file) a class with a static variable. And when I read back the data, I saw that the static variable diden't get serialized. So my conclusion is that:
static int i;
would be equivalent to
static transient int i;
Hope this helps
Dominic
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In regards to serialization, the result of having a variable defined as "static int blah" and "transient int blah" are exactly the same -- they won't be serialized.
In regards to other things (like where the variable belongs to -- static vs. non-static) they're different.
Having a variable marked as static transient is legal -- its just silly -- there's no contradiction in the behavior of the two modifiers, so why the heck would you do it?
On the otherhand, having a method marked as abstract final is down right wrong -- the definitions are totally contradictory. final means the method cannot be overridden. abstract means the definition of the method was not complete and it MUST be overriden.
does that help?
Can you think of any other modifier combinations that are illegal?
[ January 03, 2003: Message edited by: Jessica Sant ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you think of any other modifier combinations that are illegal?
private abstract
abstract static
abstract native
 
Ambapali Pal
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just the easy and common one,
abstract final
Similar to this ,
abstract
static
final
are illegal to a constructor. Even though I donot understand what is the problem with final(I did not tested it), constructors are final anyway.
Ambapali
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic