Interesting quirk, can anybody explain?

M

mdinino

In the following exampe, if I want to set the global variables of
AbstractTest in ConcreteTest, I need brackers (pointed out with "===>")
even though I am initalizing global variables in a global scope. Can
anybody tell me why the Java compiler requires this?

package Test;

public abstract class AbstractTest
{
protected int x1;
protected int x2;
protected int x3;

public void show()

{
System.out.println("x1 is: " + x1);
System.out.println("x2 is: " + x2);
System.out.println("x3 is: " + x3);
}
}


package Test;

public class ConcreteTest extends AbstractTest
{
===> {
x1 = 5;
x2 = 7;
x3 = 13;
===> }

}

package Test;

public class Test
{
public static void main(String[] args)
{
ConcreteTest myConTest = new ConcreteTest();
myConTest.show();

}

}
 
C

Chris Smith

In the following exampe, if I want to set the global variables of
AbstractTest in ConcreteTest, I need brackers (pointed out with "===>")
even though I am initalizing global variables in a global scope. Can
anybody tell me why the Java compiler requires this?

Sure. Any arbitrary code that you write in Java has to be in an
appropriate context: a method, constructor, initializer, or
initialization expression. The only thing that's allowed directly
inside of a class are declarations of that class's members. The extra
braces you pointed out are a special kind of member, known as an
instance initializer, which gets run automatically whenever an instance
of a class is created. Normally, you would write that code in a
constructor, since constructors are more familiar to most Java
programmers, but either will work.

Depending on how deep you mean the "why?" question... it has to do with
the fact that classes are declarative in nature. They don't get "run".
They simply are, and they describe the form of an object. The fact that
an object runs certain code when it is created is a proper declarative
statement. "Run this code now!" is not declarative, so it is not
appropriate in that context.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
V

VisionSet

In the following exampe, if I want to set the global variables of
AbstractTest in ConcreteTest, I need brackers (pointed out with "===>")
even though I am initalizing global variables in a global scope. Can
anybody tell me why the Java compiler requires this?
public class ConcreteTest extends AbstractTest
{
===> { // CRAZY STUFF
x1 = 5;
x2 = 7;
x3 = 13;
===> }

}

You have a perfect answer, so just to add my path through Java inheritence.

1/ What's this inheritence thing then?
2/ Oh that's nice, it makes sense.
3/ What can it do?
4/ It can do some crazy stuff and I understand it well.
5/ I'll apply some crazy stuff in something for real.
6/ Flippin' eck this is getting a bit out of hand.
7/ Read around inheritence & OO more.
8/ Use inheritence sparingly and in a simpler fashion.
9/ Next project (ok several later) == much cleaner design.

Conclusion, time spread evenly between c.l.j.* & c.l.obj is time well spent.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top