Results 1 to 9 of 9
  1. #1
    hura is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default Invoke field of a running object

    Hi all,

    I have following class:
    Java Code:
    public class A {
    	public int i;
    	
    	public int getI() {
    		return i;
    	}
    
    	public void setI(int i) {
    		this.i = i;
    	}
    
    	public static void main(String args[]) {
    		System.out.println("Hi friend");
    		A obj = new A();
    		
    		while(true){
    			obj.setI((int)(10.0 * Math.random()) + 1);
    			try {
    				Thread.sleep(1000);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}		
    	}
    }
    This class always runs, and the value of "i" is always changes.
    Now I have a class B, I wanna get the value of i, how can I do that?

    I think when class A runs, the JVM will have an instance of A, and we can invoke that instance to get the value of "i". Is it possible?

    Pls give me a point.
    Thx in advance.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    If you mean you are going to us a separate java command to "invoke" B, then have fun playing with sockets ot RMI or JMS or something similar. If they are executed from the same program, then of course.

  3. #3
    hura is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Is there any way rather than socket, RMI or JMS.
    Class B will be something like this:
    Java Code:
    public class B {
    
    	public static void main(String args[]) 
                    A classA = getClassAFromJVM();
                    int iValue = classA.getI();
    	}
    How about that?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Not from two separate JVM's. Well, there is, of course, if running in a web container, stuff like SOAP, XML, HTML, etc, of course.

  5. #5
    hura is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    @masijade: This is a normal application, just 2 normal classes.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Then execute them within the JVM (i.e. within the same program). It might help to know some more details, but if you wish to communicate between JVMs (i.e. between invocations of the "java" command) then you need real communication (Sockets, RMI, JMS, etc).

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Create an object of class B and pass your object of class A into it?
    B bObj = new B(aObj);
    ?

    Without actualyl knowing what it is you;re doing, or what relationship A and B have to each other it's rather difficult to say.

  8. #8
    hura is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Create an object of class B and pass your object of class A into it?
    B bObj = new B(aObj);
    ?

    Without actualyl knowing what it is you;re doing, or what relationship A and B have to each other it's rather difficult to say.
    There is no relationship between them.

    Yep, what I wanna do is similar to this, but aObj is passed to B is not like this way:
    A aObj = new A();
    B bObj = new B(aObj );
    because when we call new, the value of "i" will be lost. I wanna call the A instance when A is running.

    A has a main class, I call this main method to run A. While running, A will set values for some properties inside it. A runs and runs there. Now B want to get some value from A.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Of course there's a relationship.
    If there's something in A that is needed by B then there's a relationship.

    OK, I wasn;t sure before that you were talking about two separate programs. Since you are then I refer you to masijade's comments.

Similar Threads

  1. front end display of field description when clicking the field name
    By neils in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 10-29-2010, 11:47 AM
  2. onchange invoke java object method and submit form
    By marckamga in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 10-14-2010, 06:26 AM
  3. ArrayList Object Field
    By murph in forum New To Java
    Replies: 7
    Last Post: 08-23-2010, 08:25 AM
  4. Replies: 4
    Last Post: 01-17-2010, 11:13 PM
  5. Object Reflection: Getting field value
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:12 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
  •