Results 1 to 8 of 8
  1. #1
    dhia annur is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default local variable and instance variable

    Hi, i'm new using for java. I'm want ask how to change instance method to local method? please give me example

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    18,333
    Rep Power
    27

    Default Re: local variable and instance variable

    Please explain what you mean by an "instance method" and a "local method". How are they different?
    Why does the question's title refer to variable?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default Re: local variable and instance variable

    You can't create local methods in Java; you can make your methods private though. You can create local classes and interfaces though ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  4. #4
    dhia annur is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: local variable and instance variable

    i'm sorry. what you say is right. i want know how to change instance variable to local variable in method?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    18,333
    Rep Power
    27

    Default Re: local variable and instance variable

    Do you understand the difference between an instance variable and a local variable?
    If you can define each of them, that will tell you what you need to do.

    Post a very small piece of code that what shows what you want to do.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,735
    Rep Power
    7

    Default Re: local variable and instance variable

    Instance fields are store on the heap and local variables are stored on the stack. So I don't believe you can alter the nature of either.
    However, just re-assign it.

    Java Code:
    int foo = 1;// instance variable defined in class
    
    public void bar() {
       int  v = foo; // instance variable now assigned to local variable.
       int foo = this.foo; // instance variable now assigned to local variable of same name
                               // (not recommended) as it is hiding the instance variable.
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    dhia annur is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: local variable and instance variable

    I give one example question.

    public int totalCharge (int amt) {
    return (balance -
    (int) Math.round (amt * 1.5));
    }
    Rewrite the following method, using local variable?

    *i'm not sure this is instance variable or else but how to change this code to local variable?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    18,333
    Rep Power
    27

    Default Re: local variable and instance variable

    how to change this code to local variable?
    What is the name of the variable are you talking about?
    What is the definition of a local variable? How can you tell if a variable is local?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Local variable might not have been initialized
    By outforlunch in forum New To Java
    Replies: 1
    Last Post: 09-17-2012, 09:16 PM
  2. Replies: 1
    Last Post: 04-21-2012, 06:03 PM
  3. The Local variable is never read
    By Gunaseelan in forum Java Servlet
    Replies: 2
    Last Post: 01-05-2011, 10:02 AM
  4. BUG:local variable is never read
    By luckyleaf95 in forum New To Java
    Replies: 9
    Last Post: 02-03-2010, 09:23 AM
  5. Instance variable
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •