Learning Java/Objects and Classes: Difference between revisions

Content deleted Content added
→‎Fields: Cleaned up code example. Added further clarification.
Line 21:
==Fields==
Fields (also known as instance variables) are variables declared in the class itself - not in any method. For example, the following is a field:
<source lang="java">
<pre>
class SomeClass
{
public SomeClass(); //default constructor
int field;//This was not declared inside any method
private int field; //instance variable
}
</presource>
Fields are like any other variable; they may hold primitive types (int, boolean, double, etc) or reference types (String, Integer, or any other class type). Fields can be accessed outside of the class depending on the access modifier they are defined with. In the example above "field" has an access modifier of private, which means that it may only be accessed from within the class itself, and may not be accessed directly outside of the class. We will touch on access modifiers later on.
Fields have many purposes, as they are global variables. They can be accessed from outside the class in objects (see Objects).
 
==Methods==