Why first line super() only??

A

Aquila Deus

Hi all!

When I call super() in constructos, why does javac force me to put it
at the first line? Why not allow things like this:

public SomeClass(string filePath) {
File f = new File("someDir/" + filePath);
super(f);
}

?
 
B

Bent C Dalager

Hi all!

When I call super() in constructos, why does javac force me to put it
at the first line? Why not allow things like this:

public SomeClass(string filePath) {
File f = new File("someDir/" + filePath);
super(f);
}

Because the language is specified to always initialize the superclass
instance variables before initializing the derived class instance
variables. As a consequence, super's ctor must always be run before
any part of this' ctor since the ctor is generally used for instance
variable initialization.

Note, however, that you can write
super(new File("someDir/" + filePath));

Cheers
Bent D
 
R

Robert Mischke

Aquila Deus said:
When I call super() in constructos, why does javac force me to put it
at the first line? Why not allow things like this:

public SomeClass(string filePath) {
File f = new File("someDir/" + filePath);
super(f);
}

Sometimes, you can cheat around this:

public SomeClass(String filePath)
{
super(new File("someDir/" + filePath));
}



However, there are limits on which code the compiler accepts this way.
For example, accessing new fields is not allowed (which makes sense):

class SomeClass extends BaseClass
{
String localField;

public SomeClass(String filePath)
{
super(new File("someDir/" + filePath + localField));
}
}




HTH,
Robert
 
T

Tony Morris

Robert Mischke said:
Sometimes, you can cheat around this:

public SomeClass(String filePath)
{
super(new File("someDir/" + filePath));
}



However, there are limits on which code the compiler accepts this way.
For example, accessing new fields is not allowed (which makes sense):

class SomeClass extends BaseClass
{
String localField;

public SomeClass(String filePath)
{
super(new File("someDir/" + filePath + localField));
}
}




HTH,
Robert

This is what I call a "red herring".
That is, someone without a clue has made a failed attempt at providing an
answer.
For the benefit of those who hit the above post during a search (or
similar), everything within the post is fallacy i.e. all false and is not a
path worth pursuing.
 
A

Antti S. Brax

This is what I call a "red herring".
That is, someone without a clue has made a failed attempt at providing an
answer.
For the benefit of those who hit the above post during a search (or
similar), everything within the post is fallacy i.e. all false and is not a
path worth pursuing.

Please elaborate.

From what I know, the OP is correct when he claims that you can
not access member variables before calling superclass constructor.
 
F

fg

Please elaborate.

From what I know, the OP is correct when he claims that you can
not access member variables before calling superclass constructor.

I think he is...
You can also cheat if you need a better treatment of your constructor
parameters :

public SomeClass(Object1param1, Object2param2)
{
super(doSomeWork(param1, param2));
}

private *static* Object doSomeWork(Object1param1, Object2param2)
{
if (param1.xxx() && param2.yyy()) return sth;
return sthEse;
}

I insist on the fact that the called method is static : the object is not
yet initialized, so you cannot access its fields nor its instance methods.


Fred.
 
X

xarax

fg said:
I think he is...
You can also cheat if you need a better treatment of your constructor
parameters :

public SomeClass(Object1param1, Object2param2)
{
super(doSomeWork(param1, param2));
}

private *static* Object doSomeWork(Object1param1, Object2param2)
{
if (param1.xxx() && param2.yyy()) return sth;
return sthEse;
}

I insist on the fact that the called method is static : the object is not yet
initialized, so you cannot access its fields nor its instance methods.

The compiler also insists on that, because it won't allow
a usage of "this", either explicitly or implicitly, until
the parent constructor has returned. Therefore, you cannot
reference instance members within the super() parameter
list.

There is a work-around that most beginners should avoid.
That is to override a public or protected instance method
that is known to be called from a parent constructor. That
would send control back into the child instance before its
parent has been fully constructed. It's dangerous and not
for the timid, and I use it under very controlled situations
(and with plenty of documentation).
 
M

Mike Schilling

Tony Morris said:
This is what I call a "red herring".
That is, someone without a clue has made a failed attempt at providing an
answer.
For the benefit of those who hit the above post during a search (or
similar), everything within the post is fallacy i.e. all false and is not
a
path worth pursuing.

This is what I call a "useless posting". That is, someone has posted a
message devoid of any useful information.
 
R

Robert Mischke

Tony Morris said:
This is what I call a "red herring".
That is, someone without a clue has made a failed attempt at providing an
answer.

The question had already been answered. What I provided is an
additional bit of information.
For the benefit of those who hit the above post during a search (or
similar), everything within the post is fallacy i.e. all false and is not a
path worth pursuing.

Do you have any *arguments* to back this up? Or are you just trolling?

If you think I'm wrong, fine, prove it. I have no problems admitting
when I'm wrong. But by posting random insults, *you* end up looking
like an idiot, not me.

Robert
 
A

Aquila Deus

fg said:
I think he is...
You can also cheat if you need a better treatment of your constructor
parameters :

public SomeClass(Object1param1, Object2param2)
{
super(doSomeWork(param1, param2));
}

private *static* Object doSomeWork(Object1param1, Object2param2)
{
if (param1.xxx() && param2.yyy()) return sth;
return sthEse;
}

I insist on the fact that the called method is static : the object is not
yet initialized, so you cannot access its fields nor its instance
methods.

Ah-huh! Thank you for the solution :)

But doesn't javac just treat the constructor as static before it calls
super() (if it does), since it allows things like
super(doSomething(param))?
 
S

steve

The question had already been answered. What I provided is an
additional bit of information.


Do you have any *arguments* to back this up? Or are you just trolling?

If you think I'm wrong, fine, prove it. I have no problems admitting
when I'm wrong. But by posting random insults, *you* end up looking
like an idiot, not me.

Robert

forget it don't waste your time replying to him.
he is trolling.

steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top