Results 1 to 7 of 7
  1. #1
    syle_q is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default Accessing methods in a different class

    In my main class i have created a new SAXParser, once parsing is complete in the parse class i hava clalled a method on one the of the objects that was created during parsing, once inside that method that i just called, how do access a different object that was created in the parse class?

  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 syle_q View Post
    In my main class i have created a new SAXParser, once parsing is complete in the parse class i hava clalled a method on one the of the objects that was created during parsing, once inside that method that i just called, how do access a different object that was created in the parse class?
    I'm not sure how we can answer this until you give us more information. Assume that we can't read minds, that we have no idea what your code looks like and that we don't know what is not working in the code you've tried.

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Well as long as the class is in the same directory you can simply do this (as long as the thing you are trying to access isn't private):
    Java Code:
    Classname.method/variable
    Example:
    Java Code:
    Circle.measure();
    If its private then you have to create an instance of the class and refer to it like so:
    Java Code:
    Circle c = new Circle();
    c.measure();
    But if what you're trying to access is static then you'll have to use the classname.
    Last edited by Solarsonic; 03-30-2011 at 02:25 AM.

  4. #4
    syle_q is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    public void startElement(String s1, String s2, String name, Attributes attr) {
    if (name.equals("room")) {


    curRoom = findRoom(attr.getValue("name"));
    curRoom.roomDes = attr.getValue("description");
    curRoom.roomState = attr.getValue("state");

    String n = attr.getValue("north");
    if (n != null) {
    Room north = findRoom(n);
    curRoom.north = north;
    north.south = curRoom;
    }

    String s = attr.getValue("south");
    if (s != null) {
    Room south = findRoom(s);
    curRoom.south = south;
    south.north = curRoom;
    }

    String e = attr.getValue("east");
    if (e != null) {
    Room east = findRoom(e);
    curRoom.east = east;
    east.west = curRoom;
    }

    String w = attr.getValue("west");
    if (w != null) {
    Room west = findRoom(w);
    curRoom.west = west;
    west.east = curRoom;
    }

    }


    if (name.equals("animal")) {
    Animal newAnimal = new Animal(attr.getValue("name"), attr.getValue("description"));
    curRoom.addCreature(newAnimal);


    }
    if (name.equals("NPC")) {
    NPC newNPC = new NPC(attr.getValue("name"), attr.getValue("description"));
    curRoom.addCreature(newNPC);


    }
    if (name.equals("PC")) {
    PC newPC = new PC(attr.getValue("name"), attr.getValue("description"));
    curRoom.addCreature(newPC);
    newPC = user;
    user.room = curRoom;

    }
    }




    ^^^the above code is in my parse class, it parses and creates the respective objects (rooms, animals, NPC's, etc.) then once parseing is done i try to calls the play method on the PC objects names user. then once in the play method in the PC class...


    else if(command.equalsIgnoreCase("clean")) {

    if (room.roomState.equals("dirty")) {
    room.roomState = "half-dirty";
    System.out.println(room + " is now " + room.roomState + ".");
    p.play(p);
    }

    if (room.roomState.equals("half-dirty")) {
    room.roomState = "clean";
    System.out.println(room + " is now " + room.roomState + ".");
    p.play(p);
    }

    if (room.roomState.equals("clean")) {
    System.out.println("This room is already clean.");
    p.play(p);
    }

    }


    then once in that method. i need to make another method call on an "animal" object that was created in the parse class.


    The PC the user, then if the PC selects a certain demand, the "aniamal" and "NPC" objects in the same room as the PC will react accordingly to the PC's demand.






    Im new to this forum so please bare withe me, thanks.

  5. #5
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by syle_q View Post
    public void startElement(String s1, String s2, String name, Attributes attr) {
    if (name.equals("room")) {


    curRoom = findRoom(attr.getValue("name"));
    curRoom.roomDes = attr.getValue("description");
    curRoom.roomState = attr.getValue("state");

    String n = attr.getValue("north");
    if (n != null) {
    Room north = findRoom(n);
    curRoom.north = north;
    north.south = curRoom;
    }

    String s = attr.getValue("south");
    if (s != null) {
    Room south = findRoom(s);
    curRoom.south = south;
    south.north = curRoom;
    }

    String e = attr.getValue("east");
    if (e != null) {
    Room east = findRoom(e);
    curRoom.east = east;
    east.west = curRoom;
    }

    String w = attr.getValue("west");
    if (w != null) {
    Room west = findRoom(w);
    curRoom.west = west;
    west.east = curRoom;
    }

    }


    if (name.equals("animal")) {
    Animal newAnimal = new Animal(attr.getValue("name"), attr.getValue("description"));
    curRoom.addCreature(newAnimal);


    }
    if (name.equals("NPC")) {
    NPC newNPC = new NPC(attr.getValue("name"), attr.getValue("description"));
    curRoom.addCreature(newNPC);


    }
    if (name.equals("PC")) {
    PC newPC = new PC(attr.getValue("name"), attr.getValue("description"));
    curRoom.addCreature(newPC);
    newPC = user;
    user.room = curRoom;

    }
    }




    ^^^the above code is in my parse class, it parses and creates the respective objects (rooms, animals, NPC's, etc.) then once parseing is done i try to calls the play method on the PC objects names user. then once in the play method in the PC class...


    else if(command.equalsIgnoreCase("clean")) {

    if (room.roomState.equals("dirty")) {
    room.roomState = "half-dirty";
    System.out.println(room + " is now " + room.roomState + ".");
    p.play(p);
    }

    if (room.roomState.equals("half-dirty")) {
    room.roomState = "clean";
    System.out.println(room + " is now " + room.roomState + ".");
    p.play(p);
    }

    if (room.roomState.equals("clean")) {
    System.out.println("This room is already clean.");
    p.play(p);
    }

    }


    then once in that method. i need to make another method call on an "animal" object that was created in the parse class.


    The PC the user, then if the PC selects a certain demand, the "aniamal" and "NPC" objects in the same room as the PC will react accordingly to the PC's demand.






    Im new to this forum so please bare withe me, thanks.
    Please use code tags around your code.

  6. #6
    syle_q is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Java Code:
    public void startElement(String s1, String s2, String name, Attributes attr) {
    if (name.equals("room")) {
    
    
    curRoom = findRoom(attr.getValue("name"));
    curRoom.roomDes = attr.getValue("description");
    curRoom.roomState = attr.getValue("state");
    
    String n = attr.getValue("north");
    if (n != null) {
    Room north = findRoom(n);
    curRoom.north = north;
    north.south = curRoom;
    }
    
    String s = attr.getValue("south");
    if (s != null) {
    Room south = findRoom(s);
    curRoom.south = south;
    south.north = curRoom;
    }
    
    String e = attr.getValue("east");
    if (e != null) {
    Room east = findRoom(e);
    curRoom.east = east;
    east.west = curRoom;
    }
    
    String w = attr.getValue("west");
    if (w != null) {
    Room west = findRoom(w);
    curRoom.west = west;
    west.east = curRoom;
    }
    
    }
    
    
    if (name.equals("animal")) {
    Animal newAnimal = new Animal(attr.getValue("name"), attr.getValue("description"));
    curRoom.addCreature(newAnimal);
    
    
    }
    if (name.equals("NPC")) {
    NPC newNPC = new NPC(attr.getValue("name"), attr.getValue("description"));
    curRoom.addCreature(newNPC);
    
    
    }
    if (name.equals("PC")) {
    PC newPC = new PC(attr.getValue("name"), attr.getValue("description"));
    curRoom.addCreature(newPC);
    newPC = user;
    user.room = curRoom;
    
    }
    }



    the above code is in my parse class, it parses and creates the respective objects (rooms, animals, NPC's, etc.) then once parseing is done i try to calls the play method on the PC objects names user. then once in the play method in the PC class...


    Java Code:
    else if(command.equalsIgnoreCase("clean")) {
    
    if (room.roomState.equals("dirty")) {
    room.roomState = "half-dirty";
    System.out.println(room + " is now " + room.roomState + ".");
    p.play(p);
    }
    
    if (room.roomState.equals("half-dirty")) {
    room.roomState = "clean";
    System.out.println(room + " is now " + room.roomState + ".");
    p.play(p);
    }
    
    if (room.roomState.equals("clean")) {
    System.out.println("This room is already clean.");
    p.play(p);
    }
    
    }

    then once in that method. i need to make another method call on an "animal" object that was created in the parse class.


    The PC the user, then if the PC selects a certain demand, the "aniamal" and "NPC" objects in the same room as the PC will react accordingly to the PC's demand.

  7. #7
    peterhammond is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    i have the same problem accessing a method in the same package but different class???

    i get this error

    an enclosing instance that contains rainbow.rainbowView.cast is required

    Calling Class:
    Java Code:
    rainbowView method = new rainbow.rainbowView.cast();
    Class with method
    Java Code:
    public class rainbowView extends FrameView {
     
        public rainbowView(SingleFrameApplication app) {
            super(app);
    
             ..................
             ..................
    
     public StringBuffer cast() {
    
    .......
    .......
    
                 }
    public StringBuffer cast() cant be static....
    Last edited by peterhammond; 03-30-2011 at 07:47 PM.

Similar Threads

  1. Problems accessing methods in an object.
    By Kevinius in forum New To Java
    Replies: 2
    Last Post: 03-05-2011, 10:52 AM
  2. Accessing int's, String's by methods.
    By Cyloc in forum New To Java
    Replies: 2
    Last Post: 12-14-2010, 01:25 AM
  3. Replies: 5
    Last Post: 10-11-2010, 01:16 AM
  4. Accessing GUI object methods
    By km0r3 in forum AWT / Swing
    Replies: 5
    Last Post: 10-06-2010, 01:39 AM
  5. Error Accessing Methods in My JAR file
    By avu in forum Advanced Java
    Replies: 4
    Last Post: 03-23-2010, 12:35 AM

Posting Permissions

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