java - Can you access a private variable of an instance if it is in the same class? (copyOf) -


as example,

public class swapnumbers {  private int anumber = 0; ///////////////////////////////////      public swapnumbers(){      }      public void changenumber(int changed){     anumber = changed;      }     public void swap(swapnumbers othernumber){ ///////////      anumber = othernumber.anumber; //can access anumber?     }   } 

would swap in work? first instinct not work because trying access private value.

yes. making member private means available current class along of inner-classes, subject static qualifiers.

package-private (or default, without access modifiers) means available class in same package.

the public modifier makes available class in package anywhere. careful these :)


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -