File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Declaring objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declaring objects" Watch "Declaring objects" New topic
Author

Declaring objects

Onslow McCann
Greenhorn

Joined: Aug 29, 2007
Posts: 18
Hello,

When I declare my "MakePlayer" objects outside the constructor my program compiles and runs ok. However when I try to declare them within the constructor, I get errors when I use setNamePlayer method.
Is there someone that can explain this to me?





[edited to add code tags to preserve indentation]
[ October 28, 2007: Message edited by: Jeanne Boyarsky ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

Onslow,
Do you get the error from within the constructor or another method?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Onslow McCann
Greenhorn

Joined: Aug 29, 2007
Posts: 18
Hi Jeanne,

Wow, that was speedie. Thanks for that.
You made me re-examine the program and it seems the error occurs outside the constructor, at button event. How do I get around this problem, when I can't make the objects public, when I declare them within the constructor?


public void actionPerformed(ActionEvent e)
{
if (e.getSource()==jbPlayGame)
{
Chrissie.setNameActive(0);
Wichie.setNameActive(0);
Ancie.setNameActive(0);
Frankie.setNameActive(0);
Stevie.setNameActive(0);
Coenie.setNameActive(0);
Piet_Heijnie.setNameActive(0);
Strangie1.setNameActive(0);
Strangie2.setNameActive(0);
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Let's get some terms straight:

1) Declaring a variable means stating its type and its name:

MakePlayer Chrissie;

This is the definition of the variable, and where you do it determines what kind of variable it is. If you do it in the class body, it's a member variable; it's part of the object, and is available as long as the object is, from any method in the class. If you do it inside a method, however, it's a local variable, and its available only while that method is executing; as soon as the method returns, the variable disappears. It can't be accessed by any other method except the one it's defined in.

2) Assigning a value to a variable is something you can do any time after a variable is declared:

Chrissie = new MakePlayer();

If you declare a member variable in the class body, you can assign a value to it in the constructor. That's probably what you want to do here.

3) Initializing a variable means assigning a value to it at the same time as declaring it:

MakePlayer Chrissie = new MakePlayer();

This is a fine thing to do, but it's optional. You can declare a variable without initializing it. Sometimes, as here, you have a specific reason why you want to do exactly that.

I think what you want to do is declare the variables in the class body, and assign their values in the constructor.

Finally, a few more random pieces of advice: the variables shouldn't be public -- in fact, whenever possible, you want to make member variables private. Second, there's a very strong convention that Java variable names start with a lowercase letter; using uppercase as you have here makes your code harder for an experienced Java programmer to follow.


[Jess in Action][AskingGoodQuestions]
Onslow McCann
Greenhorn

Joined: Aug 29, 2007
Posts: 18
Thanks Ernest,

I'll do my best to adhere to Java naming Convention in future.

What I'm trying to achieve is to declare MakePlayer variables according to a Player.txt file I've created, as opposed to naming every player in the class itself.

I'm gonna call a getPlayerName method but am trying to work out where to call it. The program worked initially with the declaration\initiallisation within the class body but I've been unsuccesfull in using the getPlayerName method to achieve the same result.
Therefore I'm now testing the initialisation within the method, but because I need the objects outside that method I'm running into trouble.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

OK, that's a little different. You can declare them in the class body, and initialize them in the constructor, but the variable names -- all variable names[/i] -- are fixed at compile time. You can't name variables based on input data. The sooner you accept this, the sooner you can get on with your program -- this is a very common hurdle for the beginning programmer.

If you are going to read in a changing number of player definitions from a file, then you cannot have a distinct named variable to hold each player, since when the program is compiled, you don't know what that number is. What you need to do instead is have just one variable, but instead of it being a MakePlayer object, it should be either an array (MakePlayer[]) or a list (List<MakePlayer> of MakePlayers. Then you can create the array object and assign it to the variable after you've read the size from the file -- or just use an ArrayList<MakePlayer> which will grow as needed by itself.

Does this make sense?
Onslow McCann
Greenhorn

Joined: Aug 29, 2007
Posts: 18
Yes, I'm starting to catch on.

I think I'll try using ArrayList to solve the problem.

I'd already declared one in the previous example but that was as far as I'd gotten with that. I knew of their existance but have never used them before.
I've been kinda avoiding arrays altogether, but I guess it's time to face
that demon. ;-)

Thanks for your help!
 
 
subject: Declaring objects
 
Similar Threads
ArrayList
creating controls at run time
Linear Search Woes
Dialogs windows not displaying properly
NullPointerException