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 2007

Tip: Looking for answers? Try searching our database.

method for a class constructor

Thread view: 
dendeezen - 15 May 2007 09:56 GMT
Hi guys,

As a novice, I can't find out (and believe me, I tried hard),how to
write the right method to construct a class.
Hereby a SSCCE, I guess this is the best way to explain what I mean.

<code>
public class test extends JFrame{

        alfa alf;
        beta bet;
        gamma gam;

        public test () {

alfa alf = new alfa(this,null);
beta bet = new beta(this,null,null);
gamma gam = new gamma(this,null);

alfa.setbeta(bet);
bet.setalpha(alf);
bet.setgamma(gam);
gam.setbeta(bet);

        <stuff>
   }
}

class alfa extends JPanel implements ActionListener {

    test ti;
    beta bet;

    public alfa (test ti, beta bet) {

        //??
        public void setbeta( <stuff>) {
            <stuff>
            }

        this.ti = ti;
        this.bet = bet;

        <stuff>
        }

}

class beta extends JPanel implements ActionListener{

    test ti;
    alfa alf;
    gamma gam;
    public beta(test ti,alfa alf, gamma gam) {

        //??
        public void setalfa(<stuff>) {
            <stuff>
        }

        //??
        public void setgamma(<stuff>) {
            <stuff>
            }

        this.gam = gam;
        this.ti = ti;
        this.alf=alf;

        <stuff>
        }
}

class gamma extends JPanel {

    test ti;
    beta bet;

    public gamma(test ti, beta bet) {

        //??
        public void setbeta(<stuff>) {
            <stuff>
            }
        this.ti=ti;
        this.bet=bet;
    <stuff>
    }
}

</code>

Tanks,
Hendrik Maryns - 15 May 2007 10:29 GMT
dendeezen schreef:
> Hi guys,
>
> As a novice, I can't find out (and believe me, I tried hard),how to
> write the right method to construct a class.
> Hereby a SSCCE, I guess this is the best way to explain what I mean.

Well it does not compile, so one of the Cs is not appropriate.  You have
to have a look at basic class structure.  Did you read a tutorial on
Java?  You cannot define methods /inside/ constructors.  Define them in
the class body, and invoke them in the constructor.  (But then, watch
out for that typical caveat about invoking overridable methods in the
constructors.)

HTH, H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Lew - 15 May 2007 13:44 GMT
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 12 lines]
> out for that typical caveat about invoking overridable methods in the
> constructors.)

<http://java.sun.com/docs/books/tutorial/index.html>

Also, read
<http://java.sun.com/docs/codeconv/>
for things like capitalization of class names.

I personally violate their conventions with respect to placement of the
opening brace ('{'), using instead the widely-adopted convention of giving it
its own line.  (It just is better that way.)

Signature

Lew

bcr666 - 15 May 2007 14:43 GMT
This is what your code should look like

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Test extends JFrame { // Class names should begin with a
capital letter
 Alpha alpha = null;  // Alpha is spelled with a 'ph', not 'f'
 Beta beta = null;  // although not required, you should always
initialize all variables
 Gamma gamma = null;  // instance names should begin with a lowercase
letter

 public Test () {
   alpha = new Alpha(this, null);
   beta = new Beta(this, null, null);
   gamma = new Gamma(this, null);

   alpha.setBeta(beta); // method names should begin with a lowercase
letter, but capitalize all other words.
   beta.setAlpha(alpha);
   beta.setGamma(gamma);
   gamma.setBeta(beta);
 }
}

class Alpha extends JPanel implements ActionListener {
 Test test = null;
 Beta beta = null;

 public Alpha(Test test, Beta beta) {
   this.test = test;
   this.beta = beta;
 }

 public void setBeta(Beta beta) {
   this.beta = beta;
 }

 public void actionPerformed(ActionEvent actionEvent) {
   // this method is here because this class implements
ActionListener
 }
}

class Beta extends JPanel implements ActionListener {
 Test test = null;
 Alpha alpha = null;
 Gamma gamma = null;

 public Beta(Test test, Alpha alpha, Gamma gamma) {
   this.gamma = gamma;
   this.test = test;
   this.alpha = alpha;
 }

 public void setAlpha(Alpha alpha) {
   this.alpha = alpha;
 }

 public void setGamma(Gamma gamma) {
   this.gamma = gamma;
 }

 public void actionPerformed(ActionEvent actionEvent) {
   // this method is here because this class implements
ActionListener
 }
}

class Gamma extends JPanel {
 Test test = null;
 Beta beta = null;

 public Gamma(Test test, Beta beta) {
   this.test = test;
   this.beta = beta;
 }
 public void setBeta(Beta beta) {
   this.beta = beta;
 }
}
dendeezen - 15 May 2007 17:43 GMT
A clear and straightforeward answer.
I learned a lot, great help!

thanks,
Lew - 16 May 2007 03:16 GMT
> This is what your code should look like
>
[quoted text clipped - 6 lines]
> capital letter
>   Alpha alpha = null;  // Alpha is spelled with a 'ph', not 'f'

Oh, come on!  It's a variable, not a word.  Besides, perhaps the OP is a fan
of Dutch beer, or the Russian Navy Project 705 submarine class of
hunter/killer nuclear powered vessels, or really, really nifty Italian sports
cars.

Besides, "alfa" is a perfectly valid English word.
<http://en.wiktionary.org/wiki/alfa>
<http://mw1.merriam-webster.com/dictionary/alfa>

>   Beta beta = null;  // although not required, you should always
> initialize all variables

I disagree.  You should not initialize member variables to their default
values; it's just plain silly.  It's like declaring the default constructor
and no other, and making sure to call "super();" first in it.  Silly.

And, of course, we all know where opening braces should go.

Otherwise I agree with your points down the line.

Signature

Lew

Lew - 16 May 2007 04:32 GMT
> You should not initialize member variables to their default
> values; it's just plain silly.  It's like declaring the default
> constructor and no other, ... making sure to call "super();" first in it

... and taking no other action in the constructor.

>   Silly.

Signature

Lew



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.