Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Interesting quirk, can anybody explain?

Thread view: 
mdinino@gmail.com - 24 May 2006 00:56 GMT
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();
       
    }

}
Chris Smith - 24 May 2006 01:12 GMT
> 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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

VisionSet - 24 May 2006 11:43 GMT
> 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
> {
[quoted text clipped - 5 lines]
>
> }

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.

--
Mike W


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.