Results 1 to 12 of 12
  1. #1
    joe75 is offline Member
    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0

    Default object variables

    Hi i am new to java and I was wondering about object variables. Im confused about something, I am trying to create 3 objects with different first names and last names (fields) from each other and print them out on the screen. I'm using the main method also. Sounds simple but not for me :)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default Re: object variables

    You're going to have to be more specific. What have you tried? What does your code do? Where is your SSCCE?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    joe75 is offline Member
    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0

    Default Re: object variables

    lets say I had a class named footballPlayer and the variables where first name and surname. I want to make 3 football players with different names and print them on the screen.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default Re: object variables

    Okay, and what have you tried? What does your code do? Where exactly are you stuck?

    Recommended reading: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    joe75 is offline Member
    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0

    Default Re: object variables

    Java Code:
    import java.util.Scanner;
    
    public class FootballPlayer
    {
        
        static String firstName = "";
        static String lastName = "";
    
        public static void main(String[] args)
        {
            
            int menuOption;
            FootballPlayer player1 = new FootballPlayer();
            player1.setFootballPlayer("Bobby", "King");
            
            FootballPlayer player2 = new FootballPlayer();
            player2.setFootballPlayer("john", "pong");
            
            FootballPlayer player3 = new FootballPlayer();
            player3.setFootballPlayer("billy", "tuna");
                 
            System.out.println("Press 1 to view the list of players");
            
            Scanner input = new Scanner(System.in);
            
            menuOption = input.nextInt();
            
            if (menuOption == 1) {
                
                printPlayer();
            }
        }
        
        public static void setFootballPlayer(String fName, String lName)
        {
           
           firstName = fName;
           lastName = lName;
        }
        
        public static void printPlayer() 
        {
         System.out.println(firstName + lastName);
         
        }
    }
    Last edited by joe75; 12-19-2012 at 05:32 PM.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default Re: object variables

    And what does this code do? Does it compile? Does it run? Does it throw any Exceptions? Does it work? What is it missing?

    Hint: You're misusing static functions here. I recommend reading up on them, and taking out the static on everything except the main method.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    joe75 is offline Member
    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0

    Default Re: object variables

    Yeah it compiles fine, the problem is that when it prints it only prints the latest string, which is billy tuna, i wanted it to print all three. For the static I'm not sure why but it works at the moment, i had some problems compiling so i had to change them to static, I will figure that out later unless it is a problem now.

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default Re: object variables

    The static methods and variables are your entire problem. You want three separate instances of a class, yet you're using static methods and variables, which means they all share the same information.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    joe75 is offline Member
    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0

    Default Re: object variables

    so i can take out the 'static' on the main method? and everything else obviously..

  10. #10
    joe75 is offline Member
    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0

    Default Re: object variables

    Ok no you cant, I'll try and get it to work thanks anyways.

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,956
    Rep Power
    8

    Default Re: object variables

    No, the main method needs to be static, as it doesn't belong to any instance of a class. Think of it as completely separate from any class, or if it helps, actually put it in another class by itself.

    Your name variables and methods need to belong to a specific instance of a class (otherwise every FootballPlayer will have the same name), so they can't be static.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: object variables

    Quote Originally Posted by joe75 View Post
    Yeah it compiles fine, the problem is that when it prints it only prints the latest string, which is billy tuna, i wanted it to print all three. For the static I'm not sure why but it works at the moment, i had some problems compiling so i had to change them to static, I will figure that out later unless it is a problem now.
    About instance variable and static / class variable you can read it here: Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects). By reading that tutorial you will understand the different.

    Your compiling problem because you are trying to execute a non-static from a static block. In this case the main() method. The main() method for a Java application must be declared as static method. Nothing wrong with it, it is in fact the requirement to make your class executable. But, if you need to access the setFootballPlayer() method you can't just call it directly. You have to create a FootballPlayer object and call the setFootballPlayer() method from this object.

    That's basically how you work with objects. In procedural programming what you call is a function. But in OOP to access the methods you have to create an object first. Except if you define them as static methods you don't need to create an object first.

Similar Threads

  1. Object data overlapping.- Need help with static variables.
    By Anerxomounos in forum New To Java
    Replies: 3
    Last Post: 05-27-2011, 07:47 PM
  2. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  3. Access object variables
    By AcousticBruce in forum New To Java
    Replies: 6
    Last Post: 01-10-2011, 09:41 AM
  4. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  5. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •