Results 1 to 3 of 3
  1. #1
    mochajava is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default accessing variables in a method that returns a void

    hello,

    i am trying to learn java and came across this snippet of code.

    Java Code:
    public synchronized void invert() {
            red = 255 - red;
            green = 255 - green;
            blue = 255 - blue;
            name = "Inverse of " + name;
        }
    how can one acess the variables in this case? as the method has a void or null return so it wont return anything, correct?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    These variables have not been been declared within the method and thus are class fields and are visible throughout the class.

  3. #3
    mochajava is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    thanks for the instant reply and explanation.

    i am going through the oracle trails to understand java and keep on getting stuck every now and then.

Similar Threads

  1. Accessing variables from another class
    By tti123 in forum New To Java
    Replies: 3
    Last Post: 01-23-2011, 04:43 AM
  2. Inheritance - accessing private variables
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 04-26-2010, 01:21 AM
  3. Accessing Variables From a Different Class?
    By Sterling in forum New To Java
    Replies: 1
    Last Post: 04-08-2010, 05:01 PM
  4. accessing variables
    By emp in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 04:36 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
  •