Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class??

 
Joe Harry
Ranch Hand
Posts: 10091
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below one is from here

Select 2 correct options

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


Answer given as 3 and 5, 3 is ok but how come 5??
 
Manju Devarla
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Becuase Interface methods and abstract class methods cant be static
 
Ahmed Khan Mohammed
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not a constructor: it is a method that happens to have the same name as the class.
 
ashni Prakash
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

http://www.javaworld.com/javaworld/jw-10-2000/jw-1013-constructors.html
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ashni Prakash
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joe Harry
Ranch Hand
Posts: 10091
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not worth to be asked for real scjp
[ December 19, 2006: Message edited by: ramya sri ]
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not worth to be asked for real scjp,options are not clear
 
pengfei cao
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

That is all!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic