Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: maintain value of integer

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default maintain value of integer

    public int getPriority(){
    int i=0;
    return i++;
    }

    this method is called in another file, i want incremented value of i and not 0 when everytime this function is called.


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: maintain value of integer

    Check the functionality of postincrement and preincrement

    return ++i;

    will work

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,647
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: maintain value of integer

    , i want incremented value of i
    The variable i is local to the method: getPriority() and will always start with the value assigned in the method.

    Can you explain what you want that method to do? The way it is coded it will always return the same value.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: maintain value of integer

    [QUOTE=Norm;98940]The variable i is local to the method: getPriority() and will always start with the value assigned in the method.

    Can you explain what you want that method to do? The way it is coded it will always return the same value

    [Quote= shachee:] like i want output as 1 2 3 4 etc that is incremented value.. but this program will return always 1. so by session management or making i static , something like dat we can do dat thing...

    --- Update ---

    i want the output of that program as 1 2 3 4.. etc and not as 1 always...
    with ++i will it work?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,647
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: maintain value of integer

    If you want the value of i to be remembered from one call to the next as it is changed, then i must be defined outside of any method as a class variable.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: maintain value of integer

    ya i want that only.... thnx bt what will be the code for that

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,647
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: maintain value of integer

    Are you asking how to define a class variable?
    Define the variable in the class, not in a method.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: maintain value of integer

    na not that... il check the previous thng

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,647
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: maintain value of integer

    Sorry, I have no idea what you are saying.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Integer class
    By freakycoder in forum Collections and Generics
    Replies: 1
    Last Post: June 27th, 2012, 07:28 AM
  2. Use of Integer.parseInt
    By tarkal in forum Java SE APIs
    Replies: 3
    Last Post: September 8th, 2011, 03:37 AM
  3. Maintain focus on JFrame
    By nik_meback in forum AWT / Java Swing
    Replies: 1
    Last Post: December 15th, 2010, 07:49 AM