aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors and Static Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors and Static Methods" Watch "Constructors and Static Methods" New topic
Author

Constructors and Static Methods

Pete Knecht
Ranch Hand

Joined: Mar 30, 2005
Posts: 33
Hi,
In the K&B book (p.316), the following rule is included among the list for constructors:
"You can access static variables and methods, although you can use them only as part of the call to super() or this(). (Example: super(Animal.DoThings()))".

This just seems false, as the following code compiles and runs fine:

Am I missing something here (entirely possible/likely)?
Thanks, Pete.
Anuj Troy
Ranch Hand

Joined: Apr 07, 2005
Posts: 30
hii Pete

if you see page 316, the point just above what you mention ..
it says that "You cannot make a call to an instance method or an instance variable, until after the super constructor runs".

if you see your code.. you are accessing the Static variable after you are calling super()!!! which it totally legal.infact you can access any instance variable(static or not) after super has run!!

what the second point on page 316 means is that before calling super , the only variable that you can use is Static. and that also as an argument to Super() or this().

i hope i was clear

regards
anuj


SCJP 1.4, SCWCD 1.4
your eyes cannot see what your mind does not know
Pete Knecht
Ranch Hand

Joined: Mar 30, 2005
Posts: 33
Originally posted by Anuj Soumya:
hii Pete

if you see page 316, the point just above what you mention ..
it says that "You cannot make a call to an instance method or an instance variable, until after the super constructor runs".

if you see your code.. you are accessing the Static variable after you are calling super()!!! which it totally legal.infact you can access any instance variable(static or not) after super has run!!

what the second point on page 316 means is that before calling super , the only variable that you can use is Static. and that also as an argument to Super() or this().

i hope i was clear

regards
anuj


Anuj,
Thanks for the reply, but I'm not satisfied. First, yes, the previous constructor rule dealt with instance variables and methods, but that is not the issue here. Both the rule I'm questioning and my code example deal with static vars and methods. The second rule on p. 316 pretty clearly says you can only access/use static vars and methods as arguments to super() and this() calls. I've given a simple counterexample to this which seems to work.

But because a) I am a relative newbie, and b) I respect the K&B book, I'm hesitant to outright say they have stated the rule falsely until I hear some expert opinion. Thanks, Pete.
Anuj Troy
Ranch Hand

Joined: Apr 07, 2005
Posts: 30
Originally posted by Pete Knecht:


yes, the previous constructor rule dealt with instance variables and methods, but that is not the issue here. Both the rule I'm questioning and my code example deal with static vars and methods.


hii pete

Instance variables are varibale declared at class level.In your code the Static int i; is an Instance variable.that is why you are able to access it after the constructor is called.

if you read both the points together.. it will be clear that point 2 is trying to give you a special case(static!!!) where you can access an instance variable before the call to Super() finishes.

I dont think it means that the only way to access a static varibale is thru Super() call ..as parameter!!! that would be so wrong.


Regards
anuj
Pete Knecht
Ranch Hand

Joined: Mar 30, 2005
Posts: 33
Originally posted by Anuj Soumya:


hii pete

Instance variables are varibale declared at class level.In your code the Static int i; is an Instance variable.that is why you are able to access it after the constructor is called.

We're understanding the term "instance" in very different ways.


I dont think it means that the only way to access a static varibale is thru Super() call ..as parameter!!! that would be so wrong.


Regards
anuj


Well, maybe it would be wrong; that's what I'm trying to get crystal clear on.
challa
Greenhorn

Joined: Mar 28, 2005
Posts: 23
Hi pete,
I got your question and i tried in the following way
class Foo
{

Foo()
{
System.out.println(s);
}

static String s;
static void str()
{
s="good";

}
}
class Bar extends Foo
{
Bar()
{
super(Foo.str());
}
public static void main(String[] args)
{
Bar b=new Bar();

}
}

The above code did not compile showing the error
void type not allowed in super
super(Foo.str())

If i change the code by giving return type to the method str its working
see this

class Foo
{

Foo(String s) //changes here
{
System.out.println(s);
}

static String s;
static String str()
{
s="good";
return s; //change here
}
}
class Bar extends Foo
{
Bar()
{
super(Foo.str());
}
public static void main(String[] args)
{
Bar b=new Bar();

}
}


I think we can use super to call static methods but it should have a return statemnt so that a relavent super constuctor is called.Is this write else give the correct answer.One more thing we can call static methods and variables outside super also.
Pete Knecht
Ranch Hand

Joined: Mar 30, 2005
Posts: 33
Originally posted by sai purnima:

I think we can use super to call static methods but it should have a return statemnt so that a relavent super constuctor is called.Is this write else give the correct answer.One more thing we can call static methods and variables outside super also.


Okay, that's an interesting subtlety, but it makes sense. The point of calling super(SomeClass.staticMethod()) is to pass some value on to an overloaded constructor of the superclass, so I would expect a compiler glitch if I try to pass *void*, i.e. nothing.

BUT, if your last sentence is true (which was the point/question of my original post), then that constructor rule given by K&B is in error, and we should know about it. It could well mean a question or two on the exam. I've further noticed that that rule in question does not show up in their Two Minute Drill section at the end of the chapter...which also makes me think they might have meant to drop it from the main chapter as well but it just slipped through.
Thanks, Pete.
[ April 08, 2005: Message edited by: Pete Knecht ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors and Static Methods