Solved

access another class without construct the class.

Posted on 2002-04-18
27
162 Views
Last Modified: 2010-03-31
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
Comment
Question by:chencc77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 10
  • 2
  • +2
27 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6952553
Sorry I don't understand what your problem is?
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 6952731
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
 

Author Comment

by:chencc77
ID: 6952905
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 92

Expert Comment

by:objects
ID: 6952911
Just maintain a static variable referencing the last T1 that was constructed.
0
 

Author Comment

by:chencc77
ID: 6952920
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
 
LVL 92

Expert Comment

by:objects
ID: 6952927
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
 

Author Comment

by:chencc77
ID: 6952950
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
 

Author Comment

by:chencc77
ID: 6952957
error, NullPointerException in t1.Instance.get();
0
 
LVL 92

Expert Comment

by:objects
ID: 6952962
> 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
 

Author Comment

by:chencc77
ID: 6952970
i run t1 before t2. the error still the same
0
 
LVL 92

Expert Comment

by:objects
ID: 6952985
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
 

Author Comment

by:chencc77
ID: 6952989
i not understand wht is running in different JVM. i just run both class independently.

> java t1
> java t2
0
 
LVL 92

Expert Comment

by:objects
ID: 6953014
Then they are running in different JVM's.
Why don't you run both classes in the same JVM?
0
 

Author Comment

by:chencc77
ID: 6953023
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
 

Author Comment

by:chencc77
ID: 6953053
so what is ur suggestion?
0
 

Expert Comment

by:mickoo
ID: 6954939
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 6956218
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
 

Author Comment

by:chencc77
ID: 6958609
hi mickoo, how T2.java should be write as?
0
 

Expert Comment

by:mickoo
ID: 6958642
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
 

Author Comment

by:chencc77
ID: 6958652
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
 
LVL 92

Expert Comment

by:objects
ID: 6958654
> so what is ur suggestion?

There are many options open to you. RMI is one option.
0
 
LVL 92

Expert Comment

by:objects
ID: 6958657
That code will create a NEW instance of T1, and will not call the existing running daemon.
0
 

Author Comment

by:chencc77
ID: 6958658
That code will create a NEW instance of T1, and will not call the existing running daemon.
> which code? ^_^
0
 

Author Comment

by:chencc77
ID: 6958668
hi object, do u have any example to use RMI for my program? thanks alot
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7118695
chencc77 is awaiting response on follow up question.
Moondancer - EE Moderator
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 7119425
0
 
LVL 92

Expert Comment

by:objects
ID: 7119456
:-)
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question