Solved

Variables

Posted on 2006-10-24
6
177 Views
Last Modified: 2010-03-31
I have a method abc() in class XYZ { long a=0,b,c,d;

abc()
{

 if(condition A)
 { a=System.currentTimeMillis(); }

if(condition B)
{
 b=c-a;  //i am getting a= 0 when i call the method abc() second time...
}


First time when i call method condition A satisfies

when i call second time Condition B gets satisfied

How can i keep value a= what ever i got at the condition A


Thanks
0
Comment
Question by:cutie_smily
  • 2
6 Comments
 

Author Comment

by:cutie_smily
Comment Utility

when i call abc method second time value of variable a becomes zero. I want to keep the value of a when this if satisfied

if(condition A)
 { a=System.currentTimeMillis(); }
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
What does abc() return?  void?

you might rewrite a as:

   private static boolean firstTime = true;

  public void abc()
  {
       if(conditionA)
       {
             if(firstTime)
             {
                    firstTime = false;
                    a=System.currentTimeMillis();
             }
       }
...................

// now 'a' is only once assigned a value (for this object ONLY!).

;JOOP!
             
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 125 total points
Comment Utility
Correction: my solution is for ALL objects XYZ.
If you want this to happen PER OBJECT, then drop the word 'static'.

;JOOP!        
0
 

Assisted Solution

by:AdolfDaniel
AdolfDaniel earned 125 total points
Comment Utility
If you assign the attribute a a value it will not be reset to 0 except you do it.
See the following example:


public class Xyz {
      private long a, b, c;

      public void abc(boolean condition1, boolean condition2) {
            if (condition1) {
                  a = System.currentTimeMillis();
            }
            if (condition2) {
                  b = c-a;
            }
      }
      
      public long getA() {
            return a;
      }
      
      public static void main(String[] args) {
            Xyz xyz = new Xyz();
            xyz.abc(true, false);
            System.out.println("a: " + xyz.getA());
            xyz.abc(false, true);
            System.out.println("a: " + xyz.getA());
            xyz.abc(true, true);
            System.out.println("a: " + xyz.getA());
      }

}
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
endX challenge 2 48
mapAB Challlenge 35 84
Java and GPO 11 45
sites similar to codingbat to improve coding hanson skills 3 19
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now