Results 1 to 8 of 8
  1. #1
    bikashg is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default Accessing objects between classes

    I have many java classes within my program.(lets say A,B, C and D). In the main class I create an object of class B and object of class C. Now, from class B, I need to access the class B's object created in the main class. How do I do this? Please, suggest.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by bikashg View Post
    I have many java classes within my program.(lets say A,B, C and D). In the main class I create an object of class B and object of class C. Now, from class B, I need to access the class B's object created in the main class. How do I do this? Please, suggest.

    one possibility is to pass the classes to the constructor of class B, so that the class B has the reference to the other objects.

  3. #3
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    4

    Default

    Create a constructor
    public Class B (Object B){
    Object B = B;
    }

    why would you call an object to the object class anyways? I'm curious
    Last edited by Sno; 04-29-2010 at 04:06 PM. Reason: Didn't actually read the question.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    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.

  5. #5
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    Quote Originally Posted by bikashg View Post
    I have many java classes within my program.(lets say A,B, C and D). In the main class I create an object of class B and object of class C. Now, from class B, I need to access the class B's object created in the main class. How do I do this? Please, suggest.
    I've had that same problem too. Here's the (cheap) solution I put in the main class:
    Java Code:
    public static A a;
    public static B b;
    public static C c;
    public static D d;
    And there: These objects are now always accessible from other classes with:
    <main class name>.a = new A();
    <main class name>.a.someMethod();

    etc.

  6. #6
    bikashg is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    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.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    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
    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.
    }
    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.

    Also, don't make everything static.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default

    If B needs to know about C, then it will need to have an attribute of class C.
    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();
        }
    }
    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.

Similar Threads

  1. Accessing objects from within Action listener
    By cog in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 08:17 PM
  2. Classes and Objects Help
    By collin389 in forum New To Java
    Replies: 1
    Last Post: 12-14-2009, 12:44 AM
  3. Constructors Objects and Classes
    By Tykk in forum New To Java
    Replies: 4
    Last Post: 10-10-2009, 11:31 PM
  4. classes as objects
    By kroiz in forum New To Java
    Replies: 4
    Last Post: 07-25-2009, 05:22 AM
  5. Objects and Classes
    By Aleve in forum New To Java
    Replies: 8
    Last Post: 12-31-2007, 08:05 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
  •