HI all, I am having a little problem with the following program. Here are the files:
Player.java:

import java.text.DecimalFormat;

public class Player {   
    private String name;
    private double average; 

    public Player(String name, double average) {
        this.name=name;
        this.average=average;
    }

    public String getName() {
        return name;
    }

    public double getAverage() {
        return average;
    }

    public String getAverageString() {
        DecimalFormat decFormat = new DecimalFormat();
        decFormat.setMaximumIntegerDigits(0);
        decFormat.setMaximumFractionDigits(3);
        decFormat.setMinimumFractionDigits(3);
        return decFormat.format(average);    
    }
}

ShowTeamFrame.java:

import java.io.IOException;

class ShowTeamFrame {

    public static void main(String args[]) 
                               throws IOException {
        new TeamFrame();
    }
}

TeamFrame.java

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.GridLayout;

@SuppressWarnings("serial")
public class TeamFrame extends JFrame {

    public TeamFrame() throws IOException {
        Player player;
        Scanner keyboard = 
                    new Scanner(new File("Hankees.txt"));

        for (int num = 1; num <= 9; num++) {
            player = new Player(keyboard.nextLine(),
                                keyboard.nextDouble());
            keyboard.nextLine();

            addPlayerInfo(player);
        }        

        setTitle("The Hankees");
        setLayout(new GridLayout(9, 2, 20, 3));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    void addPlayerInfo(Player player) {
        add(new JLabel("  " + player.getName()));
        add(new JLabel(player.getAverageString()));
    }   
}

My problem is with the constructor: where does it get called? I would have thought in this line
Player player; but shouldn't this be Player player = new Player();?
thanks

My problem is with the constructor: where does it get called? I would have thought in this line
Player player; but shouldn't this be Player player = new Player();?

You are right in saying that the line Player player; does not call the constructor, however, if you look a bit further you see that in the for loop on line 17 (TeamFrame.java) there's a constructor call ;-)

Also notice that Player player = new Player(); would be invalid here anyhow since there's no such constructor defined in the code you posted.

Hi thanks, yes sorry I meant player = new Player(parameter1, parameter2);. Ah ok, I thought that the variable declaration Player player; and player = new Player(parameter1, parameter2); had to be in the same declaration, but I seem to understand that it is not the case
thanks

Player player; creates a reference variable that can refer to a Player object, and is initially null.
new Player(p1, p2); creates a Player object
the two things are independent of each other.

player = new Player(p1, p2); creates a Player object and sets player to refer to it, then...
player = new Player(p3, p4); creates another new Player object and sets player to refer to that one.

Once you have declared the variable you can set it to refer to any particular Player object(s) as many times as you want, anywhere you want (provided the player variable is in scope, of course).

It's no different from

String name;      // reference variable (inityially null)
...
name = "Violet";  // set to refer to a String object
...
name = "James";   // or another String object

Player player; creates a reference variable that can refer to a Player object, and is initially null.

This is only true for class variables. This does not apply to local method variables (these are not initialized to a default value when no value is specified).

The compiler will issue a warning if you try to use an uninitialized variable.

The following does not compile:

public class Test
{
    public static void main(String[] args)
    {
        String s; // local variable, not initialized automatically
        // accessed
        if (s == null)
            System.out.println("s == null => true");
    }
}

/** Compiler output:
    Test.java:6: error: variable s might not have been initialized
            if (s == null)
                ^
    1 error
*/

The following compiles though:

public class Test
{
    public static void main(String[] args)
    {
        String s; // local variable, not initialized automatically
        // not accessed
    }
}

The following compiles:

public class Test
{
    String s; // class variable
    // will be initialized automatically
    // to a default value if no value is
    // specified, default value in this
    // context means: null.

    public static void main(String[] args)
    {
        Test t = new Test();
        if (t.s == null)
            System.out.println("s == null => true");
    }
}

/** Output:

    s == null => true

*/

Yes. I was thinking of class variables, but what you say is exactly right. Thanks for that clarification.

A small correction on my previous post: change "class variables" to "class variables and instance variables".

thanks for that guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.