Results 1 to 7 of 7
- 03-30-2011, 01:53 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 12
- Rep Power
- 0
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?
-
- 03-30-2011, 02:22 AM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
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):
Example:Java Code:Classname.method/variable
If its private then you have to create an instance of the class and refer to it like so:Java Code:Circle.measure();
But if what you're trying to access is static then you'll have to use the classname.Java Code:Circle c = new Circle(); c.measure();
Last edited by Solarsonic; 03-30-2011 at 02:25 AM.
- 03-30-2011, 02:38 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 12
- Rep Power
- 0
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.
- 03-30-2011, 02:41 AM #5
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 03-30-2011, 02:56 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 12
- Rep Power
- 0
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.
- 03-30-2011, 07:39 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
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:
Class with methodJava Code:rainbowView method = new rainbow.rainbowView.cast();
public StringBuffer cast() cant be static....Java Code:public class rainbowView extends FrameView { public rainbowView(SingleFrameApplication app) { super(app); .................. .................. public StringBuffer cast() { ....... ....... }Last edited by peterhammond; 03-30-2011 at 07:47 PM.
Similar Threads
-
Problems accessing methods in an object.
By Kevinius in forum New To JavaReplies: 2Last Post: 03-05-2011, 10:52 AM -
Accessing int's, String's by methods.
By Cyloc in forum New To JavaReplies: 2Last Post: 12-14-2010, 01:25 AM -
accessing variable/methods of a instantiated object
By hariza in forum AWT / SwingReplies: 5Last Post: 10-11-2010, 01:16 AM -
Accessing GUI object methods
By km0r3 in forum AWT / SwingReplies: 5Last Post: 10-06-2010, 01:39 AM -
Error Accessing Methods in My JAR file
By avu in forum Advanced JavaReplies: 4Last Post: 03-23-2010, 12:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks