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
Solved

Variables

Posted on 2006-10-24
6
179 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: 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.

Question has a verified solution.

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

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
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:

856 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