Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Variables

Posted on 2006-10-24
6
Medium Priority
?
187 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
[X]
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
  • 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 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