method for a class constructor

D

dendeezen

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,
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGSX1ke+7xMGD3itQRAqMWAJ9PnXzSKJBN1RrzQjrueqIwmkLABgCfchsj
nzYOGJVXF+5lnWm3J21nY5E=
=t8C8
-----END PGP SIGNATURE-----
 
L

Lew

Hendrik said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

dendeezen schreef:

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.)

<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.)
 
B

bcr666

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;
}
}
 
L

Lew

bcr666 said:
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'

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>
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.
 
L

Lew

Lew said:
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.
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top