Results 1 to 8 of 8
- 04-29-2010, 01:03 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
- 04-29-2010, 01:09 PM #2
- 04-29-2010, 03:44 PM #3
Create a constructor
public Class B (Object B){
Object B = B;
}
why would you call an object to the object class anyways? I'm curiousLast edited by Sno; 04-29-2010 at 04:06 PM. Reason: Didn't actually read the question.
- 04-30-2010, 01:14 PM #4
i have a small example of Class B
Java Code:import java.util.ArrayList; import java.util.Iterator; public class ClassB { int number; public ClassB(int n) { number = n; } public void printObjects(ArrayList<ClassB> bObjects) { Iterator it = bObjects.iterator(); while (it.hasNext()) { System.out.println("Im object with number " + it.next().toString()); } } public String toString() { return String.valueOf(number); } }
this class has one constructor with an int as argument and a method that takes ArrayList of type ClassB. and this is the code of the main class
Java Code:import java.util.ArrayList; public class ThisExample { public static void main(String[] args) { ArrayList<ClassB> bObjects = new ArrayList<ClassB>(); ClassB b = new ClassB(1); bObjects.add(b); bObjects.add(new ClassB(2)); bObjects.add(new ClassB(3)); b.printObjects(bObjects); } }
three objects of ClassB are instantiated and added to the arraylist and then passed to the printObjects method of the classB, so that this method can print the number of each object.
if you want to pass different objects to ClassB (ClassA, ClassC, ClassD etc.) then use ArrayList<Object> and if you need to invoke methods of an object inside the ClassB you have to use instanceof and casting the Object to the object.
- 04-30-2010, 05:40 PM #5
Member
- Join Date
- Oct 2009
- Location
- Rotterdam
- Posts
- 52
- Rep Power
- 0
I've had that same problem too. Here's the (cheap) solution I put in the main class:
And there: These objects are now always accessible from other classes with:Java Code:public static A a; public static B b; public static C c; public static D d;
<main class name>.a = new A();
<main class name>.a.someMethod();
etc.
- 05-06-2010, 10:57 AM #6
Member
- Join Date
- Apr 2010
- Posts
- 13
- Rep Power
- 0
Guys, I made a small typo during posting; so I am in trouble,again. Please help me again. This time I am typing it correctly(Sno was confused due to the typo.). Here is the exact thing that I wanted to post:
"I have many java classes within my program.(lets say A,B and C). Lets say, A is the main class. In A, I create an object of class B(say b1) and object of class C(say c1). In class B, i have some methods which need to access the c1 object's method . How do I do this? Please, suggest."
sorry for the type. Thanks to J2ME64. Please help again.
Arnold: yes, that could be "cheap" way, making static objects; but do u think this is the only way? If yes, i would be taking your solution.
- 05-06-2010, 11:42 AM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
This kind of question arises often. It is caused by lack of design.
When you create classes you are modeling real world objects and relationships between those objects.
How your classes interact to call each other's methods is determined by the relationships between your classes.
e.g
Design your classes, give them meaningful names and the method to use should be obvious. Without knowing what objects your classes represent it is impossible to say which approach you should take.Java Code:class B { int bMethod() { return 0; } } class C { protected void cMethod() { } } // A is a C class A extends C { // A-has-a-B B b; // A is created from a B public A(B b) { this.b = b; } // method aMethod uses A's B int aMethod() { return b.bMethod() + 42; } // A has a B that it can update public void setB(B b) { this.b = b; } // method process of A needs a B to do it's work // You don't need to have a B in A for this public void process(B b) { } // A is already a C so it already has the cMethod // if it needs to use it. }
Also, don't make everything static.
- 05-06-2010, 12:05 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
If B needs to know about C, then it will need to have an attribute of class C.
You could pass c into the method directly, rather than hold it as an attribute. Which one is best is entirely down to your requirements, and your model.Java Code:public class B { private C c; // Possibly passed in through a constructor? public B(C c) { this.c = c; } // Used in this method public void doSomethingToC() { c.method(); } }
Similar Threads
-
Accessing objects from within Action listener
By cog in forum New To JavaReplies: 4Last Post: 12-24-2009, 08:17 PM -
Classes and Objects Help
By collin389 in forum New To JavaReplies: 1Last Post: 12-14-2009, 12:44 AM -
Constructors Objects and Classes
By Tykk in forum New To JavaReplies: 4Last Post: 10-10-2009, 11:31 PM -
classes as objects
By kroiz in forum New To JavaReplies: 4Last Post: 07-25-2009, 05:22 AM -
Objects and Classes
By Aleve in forum New To JavaReplies: 8Last Post: 12-31-2007, 08:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks