Solved

Variables

Posted on 2006-10-24
6
180 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
ID: 17796917

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
ID: 17798359
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
ID: 17798381
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
ID: 17799409
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Way to decrease size of apk file 9 104
login jsp example 24 84
Why doesn't this text field show up on my Applet frame? 2 27
wild fly 8 startup error 2 37
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

685 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