• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Abstract class??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Becuase Interface methods and abstract class methods cant be static
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not worth to be asked for real scjp,options are not clear
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
My, my, aren't you a big fella. Here, have a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic