Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Troubles with toString and static and non-static

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Troubles with toString and static and non-static

    Ok, in this assignment I have to write the code for a game that involves picking up and putting down stones in the right combination in order to unlock the treasure chest.

    We've been advised that it only needs 4 classes i.e Player, Game, TreasureChest and Stones

    My problem comes when I try to get the Game class to run a toString method that when it's finished should return something like this "Jack(0) #(1) @(2) %(3) $(4) Treasure/6\(5)"

    This is what my code looks like for the Game class so far:

    import java.util.Scanner;
    public class Game
    {
       private int maxMoves;
       private int combination;
       private String name;
       private TreasureChest chest;
       private Player player;
     
     
       public Game()
       {
           Game game1 = new Game();
           Scanner keyboard = new Scanner(System.in);
           System.out.print("Enter combination for the treasure chest (5-10): ");
           int combination = keyboard.nextInt();
           keyboard.nextLine();
           chest = new TreasureChest(combination);
           System.out.print("Enter maximum allowed moves: ");
           int maxMoves = keyboard.nextInt();
           keyboard.nextLine();
           System.out.print("Enter player name: ");
           String name = keyboard.next();
           keyboard.nextLine();
           game1.toString();
     
       }
     
       public String toString()
       {
           return name + "";
       }
     
    }

    I still need to add the other information that the toString method is supposed to return, but I can't get it to return even the name. When I try to initialise the Game class it is supposed to ask for the combination, max moves and player name. Instead I get the error "java.lang.StackOverflowError:null"

    Any pointers?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Troubles with toString and static and non-static

    I'm not really sure what this has to do with toString or the static keyword.

    In your Game constructor, you create a new Game, which calls the Game constructor, which creates a new Game, which...

    Hint: Put a System.out.println("here"); or something as the first line in your Game constructor.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Troubles with toString and static and non-static

    Ok, that makes sense. If I remove that first line of code what do I need to change the "game1.toString(); line to for the method to actually run? That was where the static and non-static issues come from.

    If I change it to Game.toString(); I get the error message "non-static method toString() cannot be referenced from a static context."

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Troubles with toString and static and non-static

    Quote Originally Posted by BadgerWatch View Post
    Ok, that makes sense. If I remove that first line of code what do I need to change the "game1.toString(); line to for the method to actually run? That was where the static and non-static issues come from.

    If I change it to Game.toString(); I get the error message "non-static method toString() cannot be referenced from a static context."
    When you're in the constructor, you're already "in" an instance of Game, so you don't have to refer to a game instance at all. You could use this.toString(), but the "this" part is implied, so simply calling toString() will do it. Calling Game.toString() is looking for a static method called toString, which you don't have.

    But also, you're not doing anything with the String returned from the toString() method. I assume you want to print it out or something?
    Last edited by KevinWorkman; September 21st, 2011 at 08:21 AM.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Troubles with toString and static and non-static

    Oh of course. Yeah eventually, once I get the rest of the code up to scratch it is supposed to print out "Jack(0) #(1) @(2) %(3) $(4) Treasure/6\(5)" to show the player what the game situation is.

    Thanks for the quick responses I'll get back into it and see how it goes ^_^

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Troubles with toString and static and non-static

    Cool, glad you got it sorted.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    BadgerWatch (September 21st, 2011)

  8. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Troubles with toString and static and non-static

    I'm back -_-

    When I try to get the toString to return the "name" all it returns is null.

     
    import java.util.Scanner;
    public class Game
    {
       private int maxMoves;
       private int combination;
       private String name;
       private TreasureChest chest;
       private Player player;
     
     
       public Game()
       {
           Scanner keyboard = new Scanner(System.in);
           System.out.print("Enter combination for the treasure chest (5-10): ");
           int combination = keyboard.nextInt();
           keyboard.nextLine();
           chest = new TreasureChest(combination);
           System.out.print("Enter maximum allowed moves: ");
           int maxMoves = keyboard.nextInt();
           keyboard.nextLine();
           System.out.print("Enter player name: ");
           String name = keyboard.next();
           keyboard.nextLine();
           System.out.println(toString());
           turn();
           String move = keyboard.nextLine();
           move.charAt(0);
     
       }
     
       public String toString()
       {
           return "" + name + "string1" + "string2" + "string3";
       }
     
       public void turn()
       {
           System.out.print("Move (l/r/p/d) : ");
       }
    }

  9. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Troubles with toString and static and non-static

    The reason is, you have a "name" variable already declared in the class private members.
    You declare it again
    String name = keyboard.next();
    here.
    So, your name is in this variable. And when toString() tries to get value, it goes to the private variable of the class which is null.
    Note: Remove the String keyword from
    String name = keyboard.next();
    this statement and hopefully it will work all fine.

Similar Threads

  1. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM
  2. Non-Static variables
    By liloka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 31st, 2010, 08:13 AM
  3. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 06:51 PM
  4. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  5. Static to non-static - Organization
    By copeg in forum Object Oriented Programming
    Replies: 5
    Last Post: December 22nd, 2009, 12:56 PM