Method | function member of class |
Variables | data member of class |
Field | Variables and methods of a class |
class FieldExample { public int sampleData = 10; public void sampleMethod() { System.out.println( " In sample " ); }; } class CreateObject { public static void main( String args[] ) { System.out.println( " Start main " ); FieldExample test = new FieldExample ( ); test.sampleMethod(); System.out.println( test.sampleData ); } }
class AccessLevels { public int publicObjectVariable ; protected float protectedObjectVariable = 10; private int[] privateObjectVariable; int noExplicitAccessLevel = publicObjectVariable; public AccessLevels ( int startValue ) { System.out.println( " Start Constructor" ); privateObjectVariable = new int[ startValue ]; } public void sampleMethod( int value ) { System.out.println( " In method" ); privateObjectVariable[ 1 ] = value; } } class TestAccessLevels { public static void main( String args[] ) { AccessLevels test = new AccessLevels ( 11 ); test.publicObjectVariable = 100; // Ok test.protectedObjectVariable= 100; // Ok test.privateObjectVariable[ 1 ] = 100; // Compile Error test.noExplicitAccessLevel = 100; // Ok } }
class Student { public char grade; }
class PointerTest { public static void main( String args[] ) { Student sam = new Student(); sam.grade = 'A'; Student samTwin = sam; samTwin.grade = 'C'; System.out.println( sam.grade ); } }
class Parameter { public void noChangeArgument( char grade ) { grade = 'A'; }; }; class TestParameter { public static void main( String args[] ) { char samGrade = 'C'; Parameter Test = new Parameter(); Test.noChangeArgument( samGrade ); System.out.println( samGrade ); }; };
C
class TestParameter { public static void main( String args[] ) { char samGrade = 'C'; samGrade = changeGrade( samGrade ); System.out.println( samGrade ); }; public static char changeGrade( char grade ) { switch ( grade ) { case 'A': case 'B': return 'A'; case 'C': return 'B'; case 'D': case 'F': return 'C'; }; return 'A'; }; };
class TestParameter { public static void main( String args[] ) { char[] classGrades = new char[ 30 ]; classGrades[ 1 ] = 'B'; TestParameter.changeArgument( classGrades ); System.out.println( classGrades[ 1 ] ); }; static public void changeArgument( char[] grade ) { grade[1] = 'A'; }; };
class Student { public char grade; }; class Parameter { public void changeArgument( Student lucky ) { lucky.grade = 'A'; }; }; class TestParameter { public static void main( String args[] ) { Student sam = new Student(); sam.grade = 'B'; Parameter Test = new Parameter(); Test.changeArgument( sam ); System.out.println( sam.grade ); }; };
class StaticFields { public static int size = 10; protected static int[] classArray = new int [ size ]; public int localInt; static { size = 20; }; public static void classMethod( int value ) { System.out.println( " In class method" ); size = value; localInt = value; // Compile Error } } class TestStaticFields { public static void main( String args[] ) { StaticFields test = new StaticFields ( ); StaticFields secondObject = new StaticFields ( ); StaticFields.size = 100; System.out.println( test.size ); // Print 100 StaticFields.localInt = 99; // Compile Error test.localInt = 99; } }
class Constants { public static final int SIZE = 10; protected static final int[] CLASS_ARRAY = new int [ size ]; public final int MUST_INITIALIZE_HERE; // Compile Error public final char grade = 'A'; } class TestConstants { public static void main( String args[] ) { System.out.println( Constants.SIZE ); // Prints 10 Constants test = new Constants(); System.out.println( test.grade ); // Prints A System.out.println( Constants.grade ); // Compile Error } }