Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Variables

Posted on 2006-10-24
6
Medium Priority
?
189 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 500 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 500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

971 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