1. The class TestClass cannot be declared abstract 2. The variable j cannot be declared transient 3. The variable k cannot be declared synchronized 4. The constructor TestClass( ) cannot be declared final 5. The method f( ) cannot be declared static
Becuase Interface methods and abstract class methods cant be static
Ahmed Khan Mohammed
Greenhorn
Joined: Dec 07, 2006
Posts: 13
posted
0
Thats because the variable j is non static and cannot be accessed from a static context.
Followup to the comment : An abstract class can contain static methods.
ashni Prakash
Ranch Hand
Joined: Dec 05, 2006
Posts: 50
posted
0
In the above code given by jothi how come the constructor has return type.As per my knowledge constructors doesnt have any return type not even void.Can anyone clear my doubt please?
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
It is not a constructor: it is a method that happens to have the same name as the class.
But in the 4th option they have said its a constructor.The below link has given differences between constructors and methods which says methods cant have same name as class.
The below link has given differences between constructors and methods which says methods cant have same name as class.
Actuallly the article doesn't say that at all.
Constructors have the same name as their class; by convention, methods use names other than the class name.
Emphasis mine.
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
ashni Prakash
Ranch Hand
Joined: Dec 05, 2006
Posts: 50
posted
0
hi ranchers,
I executed the code successfully.As per my understanding by java convention it is not advisable to use classname as method name but it wont throw errors if we use it. [ December 19, 2006: Message edited by: nike sama ]
Thanks ranchers. I didn't notice the use of non-static variable from a static context when I was answering this question. Thanks all for the participation.
Pratap koritala
Ranch Hand
Joined: Sep 27, 2006
Posts: 251
posted
0
not worth to be asked for real scjp [ December 19, 2006: Message edited by: ramya sri ]
Pratap koritala
Ranch Hand
Joined: Sep 27, 2006
Posts: 251
posted
0
not worth to be asked for real scjp,options are not clear
pengfei cao
Greenhorn
Joined: Dec 18, 2006
Posts: 4
posted
0
Try this code:
abstract class TestClass { transient static int j; synchronized static int k; final void TestClass(){} static void f() { k = j++; } }
The result is
modifier synchronized not allowed here synchronized static int k; ^ 1 error