Results 1 to 8 of 8
  1. #1
    sdwinder is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default Problem with class instances

    I always run into this problem for almost every single java program I write, and to this day I do not know what is wrong.

    As an example, lets pretend im doing a binary search tree, I have the BST class seperate from my Main class.

    The main class has certain numbers already saved, so what the main class does, is it creates an instance of the BST class called Root ( The bst class has a data field, and 2 BST instances itself, like a linked list, one for the left child, and one for the right child, so thats all set)

    Now, what the Main class does, is it takes a single int value, and passes it into a method in the Main class called addValue().

    addValue then uses an algorithm for binary search trees to add the value to the BST.

    The problem I am running into is, that even tough an instance of the BST class exists in the main method of the main class, the addValue method in the main class cannot access it

    Main method has something like this

    BST root = new BST(); ( BST being a class)

    Then the addValue method takes the value "x" that it has been passed and attempts to do this.

    int value = x;
    root.data=value (data is an int field in the BST class)


    And basically it tells me that it has no idea what root is....Is this a limitation of java or what?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sdwinder View Post
    And basically it tells me that it has no idea what root is....Is this a limitation of java or what?
    No, I'm afraid it's a limitation in your understanding of Java, and that's OK because we all have to start from somewhere, and this can be fixed with study. Java will only do what it's told to do. Likely you have a scoping issue, but without code, who knows?

  3. #3
    sdwinder is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    OK well here is a sample piece of code that described my current issue.


    Java Code:
    public class BST {
    	public static void main(String args[]){
    		Treenode bst = new Treenode();
    
                    addValue(5);
    
              }
    
           public static void addValue(int x){
           
           int input = x;
           bst.data=input;  // this is where it says bst cannot be resolved
    }
    
    class Treenode{
             int data;
    	Treenode left;
    	Treenode right;

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    exactly as I thought: you have a scoping issue Your bst variable is declared and initialized within the main method and is visible only within the main method. If you want it visible outside of main, it must be declared outside of main, or pass bst as a parameter of the method. Don't worry, as you get more familiar with Java, you'll have less of these problems. Just don't blame the language for this.

  5. #5
    sdwinder is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    oh no, not blaming the language, just wanted to make sure it wasnt language related, like i was doing something that java does do, i figured it would be something along those lines, if i make a separate method that pretty much solely creates an instance of the class, can i just leave it at that and be able to modify it and use it with other methods in that class? or will it be onlu usable by the method that created it.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For instance:
    Java Code:
    public class Spam {
      private Baz bazField;
      
      public Spam() {
        Baz bazLocalVariable = new Baz();
        bazField = new Baz();
        
        addToBazFieldData(5);
        addToBazLocalVarData(5);
      }
      
      // this works becase bazField was declared in the class and is
      // visible throughout the class
      public void addToBazFieldData(int number) {
        int data = bazField.getData() + number;
        bazField.setData(data);
      }
      
      // this won't work since bazLocalVariable is only visible in 
      // the BST constructor
      public void addToBazLocalVarData(int number) {
        int data = bazLocalVariable.getData() + number;
        bazLocalVariable.setData(data);
      }
    }
    
    class Baz {
      private int data;
      public int getData() {
        return data;
      }
      public void setData(int data) {
        this.data = data;
      }
    }

  7. #7
    sdwinder is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    ahhhh i understand now thank you, I assume that this same concept works in the main class, where i would be able to declare the bazfield variable before the main method inside the main class and be able to see it everywhere inside the main class.

  8. #8
    literallyjer is offline Member
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by sdwinder View Post
    ahhhh i understand now thank you, I assume that this same concept works in the main class, where i would be able to declare the bazfield variable before the main method inside the main class and be able to see it everywhere inside the main class.
    That is not completely true. The main method is a static method, which means that it has slightly different rules than a non-static method. Ignoring static methods, what Fubarable said is true. However, a static method cannot access non-static instance variables, and therefore you would need to mark every instance variable static. However, doing so is bad practice due to the nature of static methods/variables.

Similar Threads

  1. Array of instances using Math.random()
    By xgi1008 in forum New To Java
    Replies: 16
    Last Post: 01-25-2011, 11:10 PM
  2. Build Instances problem
    By leapinlizard in forum New To Java
    Replies: 4
    Last Post: 04-21-2009, 10:17 AM
  3. Calling a method for all instances of an object
    By rattle in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 02:10 PM
  4. Object Reflection: Creating new instances
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:13 PM
  5. Naming object instances
    By oldgit in forum New To Java
    Replies: 9
    Last Post: 02-07-2008, 11:18 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
  •