Results 1 to 7 of 7
  1. #1
    nevinvj24 is offline Member
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Post Trace methods calls

    Consider the two simple Java classes below:

    Java Code:
    class Computer {
      Computer() {
      System.out.println("Constructor of Computer class.");
      }
      void On() {
      System.out.println("PC turning on...");
      }
      void working() {
      System.out.println("PC working...");
      }
      void Off() {
      System.out.println("PC shuting down...");
      }
      public static void main(String[] args) {
      Computer my = new Computer();
      Laptop your = new Laptop();
      my.On();
      my.working();
      your.On();
      your.working();
      my.Off();
      your.Off();
      }
    }
    Java Code:
    class Laptop {
      Laptop() {
      System.out.println("Constructor of Laptop class.");
      }
      void On() {
      System.out.println("Laptop turning on...");
      }
      void working() {
      System.out.println("Laptop working...");
      }
      void Off() {
      System.out.println("Laptop shuting down...");
      }
    }
    After the program run, how do I trace (1) which object call which method (2) and how many times?

    Thanks for any suggestion.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Trace methods calls

    You need to think! Focus on a single method. What can you add to a class that will keep a record of how many times that method is called?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Trace methods calls

    how do I trace (1) which object call which method
    The bold part is probably what makes it more challenging than dumbly counting method calls.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Trace methods calls

    Quote Originally Posted by gimbal2 View Post
    The bold part is probably what makes it more challenging than dumbly counting method calls.
    I assumed the OP wanted to keep track of how many times each object called each method. Multiple instance counters in each class would seem to work. So I figured that was sufficient. Thus, the number of times my called working() would be different than the number of times your called working().

    Perhaps I misunderstood the requirement.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    nevinvj24 is offline Member
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Thumbs up Re: Trace methods calls

    I think that I will have to use static fields to keep count.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Trace methods calls

    Quote Originally Posted by nevinvj24 View Post
    I think that I will have to use static fields to keep count.
    Based on your requirement to count method calls from objects that probably won't work (or at best, won't scale).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Trace methods calls

    Why not use a Proxy class that does the counting? (I mean the Proxy class in the java.lang.reflect package).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Trace table help
    By anthonykc1 in forum New To Java
    Replies: 1
    Last Post: 02-20-2014, 08:52 PM
  2. Gremlins in my code, I can't seem to trace
    By LaughingSeraph in forum New To Java
    Replies: 9
    Last Post: 03-14-2013, 01:59 PM
  3. Error stack trace
    By Arnold in forum Android
    Replies: 1
    Last Post: 05-13-2010, 05:54 PM
  4. Can't trace where the error, need help
    By Hnin Hnin in forum Java Servlet
    Replies: 1
    Last Post: 03-16-2009, 03:24 PM
  5. Stack Trace
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-10-2007, 05:29 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •