Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

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.
0
chencc77
Asked:
chencc77
  • 12
  • 10
  • 2
  • +2
1 Solution
 
objectsCommented:
Sorry I don't understand what your problem is?
0
 
tomboshellCommented:
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
0
 
chencc77Author Commented:
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?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
objectsCommented:
Just maintain a static variable referencing the last T1 that was constructed.
0
 
chencc77Author Commented:
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.
0
 
objectsCommented:
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();
0
 
chencc77Author Commented:
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();
  }
}


0
 
chencc77Author Commented:
error, NullPointerException in t1.Instance.get();
0
 
objectsCommented:
> 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.
0
 
chencc77Author Commented:
i run t1 before t2. the error still the same
0
 
objectsCommented:
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.
0
 
chencc77Author Commented:
i not understand wht is running in different JVM. i just run both class independently.

> java t1
> java t2
0
 
objectsCommented:
Then they are running in different JVM's.
Why don't you run both classes in the same JVM?
0
 
chencc77Author Commented:
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. =)
0
 
chencc77Author Commented:
so what is ur suggestion?
0
 
mickooCommented:
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

0
 
MoondancerCommented:
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.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://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.
http://www.experts-exchange.com/questions/Q.20139778.html
http://www.experts-exchange.com/questions/Q.20181745.html
http://www.experts-exchange.com/questions/Q.20281853.html
http://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 http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> http://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
0
 
chencc77Author Commented:
hi mickoo, how T2.java should be write as?
0
 
mickooCommented:
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

0
 
chencc77Author Commented:
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?
0
 
objectsCommented:
> so what is ur suggestion?

There are many options open to you. RMI is one option.
0
 
objectsCommented:
That code will create a NEW instance of T1, and will not call the existing running daemon.
0
 
chencc77Author Commented:
That code will create a NEW instance of T1, and will not call the existing running daemon.
> which code? ^_^
0
 
chencc77Author Commented:
hi object, do u have any example to use RMI for my program? thanks alot
0
 
MoondancerCommented:
chencc77 is awaiting response on follow up question.
Moondancer - EE Moderator
0
 
objectsCommented:
:-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 12
  • 10
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now