Link to home
Start Free TrialLog in
Avatar of chencc77
chencc77

asked on

access another class without construct the class.

i have two independate class, T1 and T2.

//****** T1.class
public class T1 {
    private int contents=2;
    private boolean available = false;
    public T1() {
          while (available==false)
          {
          }
     }
     public int get() {
        available = false;
        return contents;
    }

    public void put(int value) {
        contents = value;
        available = true;
    }

    public static void main(String[] args) {
        T1 t1 = new T1();
    }
}
//*** T1 End

//********** T2.class
public class T2 {
     
     public T2() {
     //     t1.put(10);
          System.out.println("value of contents in t1: " + t1.get());
     }
    public static void main(String[] args) {
        T2 t2 = new T2();
    }
}
//********** T2 End

T1 and T2 will be run indepently. i will run T1 before T2. My problem is, how can i get the value t1.get() ? I must run those two classes at different time.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Sorry I don't understand what your problem is?
Avatar of tomboshell
tomboshell

Not  problem.  You are calling t1.get() but there seems to be no instantiation of that variable.  You should create a new constructor for your T1 class that doesn't include an endless loop, set the boolean value to true sometime.  Then use that variable....Something like the following...
//****** T1.class
public class T1 {
   private int contents=2;
   private boolean available = false;
   public T1() {
         while (available==false)
         {
         }
    }
    public T1(boolean ref){
          available = ref;
     }
    public int get() {
       available = false;
       return contents;
   }

   public void put(int value) {
       contents = value;
       available = true;
   }

   public static void main(String[] args) {
       T1 t1 = new T1();
   }
}
//*** T1 End

//********** T2.class
public class T2 {
    private T1 t1 = new T1(true);
    public T2() {
    //     t1.put(10);
         System.out.println("value of contents in t1: " + t1.get());
         t1.put(55);
          System.out.println("value of contents in t1: " + t1.get());
    }
   public static void main(String[] args) {
       T2 t2 = new T2();
   }
}
//********** T2 End
Avatar of chencc77

ASKER

Thanks. Actually what i want to do is, T1 is a daeman process, it will be run all the time. T1 act as a Schedule and contain a vector to keep all the running time for all tasks. T2 is used to update the vector in T1. T2 is only run whenever running time is need to update. so T1 should get the lastest running time for a task. If i instantiate T1 in T2, it will start another Schedule, it may not be correct, am i right?
Just maintain a static variable referencing the last T1 that was constructed.
Sorry, let me make my question more clear. I have two class, Schedule and SchUpdate. Schedule is run as a daemon process, it contains a vector saving running time. SchUpdate is run whenever running time updating is needed. So the point is, how SchUpdate detect the existing Schedule program that is running and change the value of vector.
thanks for help.
Just have Schedule maintain a static reference to itself:

class Schedule
{
  public static Scedule Instance;

  public Schedule()
  {
     Instance = this;
     ...
  }
...
}

Then you can simply access Schedule using:

Schedule.Instance.whatever();
in Schedule.Instance.whatever();
did u mean whatever() is method in Schedule? ( get & put)

i change to this:
public class t1 {
  private int contents=2;
  private boolean available = false;
  public static t1 Instance;

  public t1() {
       Instance = this;
        while (available==false)
        {
        }
   }
   public t1(boolean ref){
         available = ref;
    }
   public int get() {
      available = false;
      return contents;
  }

  public void put(int value) {
      contents = value;
      available = true;
  }

  public static void main(String[] args) {
      t1 tt1 = new t1();
  }
}

public class t2 {
   public t2() {
        System.out.println("value of contents in t1: " + t1.Instance.get());
   }
  public static void main(String[] args) {
      new t2();
  }
}


error, NullPointerException in t1.Instance.get();
> did u mean whatever() is method in Schedule? ( get & put)

yes.

> error, NullPointerException in t1.Instance.get();

Yes, because your scheduler has not been started.
It makes no sense to call the object before it is created.
i run t1 before t2. the error still the same
Are they running in different JVM's?
If so you'll have to use RMI or something similiar, or alternatively run them in the same JVM.
i not understand wht is running in different JVM. i just run both class independently.

> java t1
> java t2
Then they are running in different JVM's.
Why don't you run both classes in the same JVM?
i prefer run in same JVM too. but Schedule might be a daemon process, while SchUpdate is only run when update is needed. simple say is, Schedule is back-end process, while SchUpdate is front-end process. =)
so what is ur suggestion?
Make your constructor protected and maintain a reference to itself.
To get a instacne of the class always use a instanceOf method
Your class would be something like this
//****** T1.class
public class T1 {
   private int contents=2;
   private boolean available = false;
   protected static T1 instance;
   protected  T1() {
         while (available==false)
         {
         }

    instance = this;
    }
    public int get() {
       available = false;
       return contents;
   }

   public void put(int value) {
       contents = value;
       available = true;
   }

   public static void main(String[] args) {
       T1 t1 = new T1();
   }

   public static T1 instanceof()
   {
      if( instance == null)
         instance = new T1();
      return instance;

}
//*** T1 End

ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.20139778.html
https://www.experts-exchange.com/questions/Q.20181745.html
https://www.experts-exchange.com/questions/Q.20281853.html
https://www.experts-exchange.com/questions/Q.20291220.html




*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations if this item remains inactive another seven (7) days.  If you are interested in the cleanup effort, please click this link https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> https://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
hi mickoo, how T2.java should be write as?
public class T2 {
   
    public T2() {
    T1 t1 = T1.instanceOf();
         System.out.println("value of contents in t1: " + t1.get());
    }
   public static void main(String[] args) {
       T2 t2 = new T2();
   }
}
//********** T2 End

i compile the code, but dun have output.
i change following code :

public static T1 instanceof()
  {
     if( instance == null)
        instance = new T1();
     return instance;

TO :

public static T1 instanceOf()
{
     if( instance == null)
        instance = new T1();
     return instance;
}

is correct, right?
> so what is ur suggestion?

There are many options open to you. RMI is one option.
That code will create a NEW instance of T1, and will not call the existing running daemon.
That code will create a NEW instance of T1, and will not call the existing running daemon.
> which code? ^_^
hi object, do u have any example to use RMI for my program? thanks alot
chencc77 is awaiting response on follow up question.
Moondancer - EE Moderator
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial