|
CS 596 Java Programming Fall Semester, 1998 Classes (2): References & Static |
|
|---|---|---|
|
© 1998, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 19-Sep-98 |
public class Student
{
public char grade;
}
public 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 );
}
}
C
public class Student
{
public char grade;
}
public class TestParameter
{
public static void main( String args[] )
{
Student sam = new Student();
sam.grade = 'C';
changeReference( sam );
System.out.println( sam.grade );
changeState( sam );
System.out.println( sam.grade );
}
public static void changeState( Student lucky )
{
lucky.grade = 'A';
}
public static void changeReference( Student unlucky )
{
unlucky = new Student();
unlucky.grade = 'A';
}
}
C A
public class StaticFields
{
public static int size = 10;
public void increaseSize( int increment )
{
size = size + increment;
}
}
public class DemoStaticFields
{
public static void main( String[] arguments )
{
System.out.println( "Size " + StaticFields.size );
StaticFields top = new StaticFields();
StaticFields bottom = new StaticFields();
top.size = 20;
System.out.println( "Size " + bottom.size );
}
}
Size 10 Size 20
public class StaticMethods
{
public static int size = 10;
public static void increaseSize( int increment )
{
size = size + increment;
}
}
public class DemoStaticMethods
{
public static void main( String[] arguments )
{
StaticMethods.increaseSize( 30 ); //Note use of class name
System.out.println( "Size " + StaticMethods.size );
StaticMethods top = new StaticMethods();
top.increaseSize( 20 );
System.out.println( "Size " + StaticMethods.size );
}
}
Size 40 Size 60
public class StaticFields {
public static int size = 10;
static { // Run when class is first loaded
size = classMethod( 20);
System.out.println( size );
}
public static int classMethod( int value ) {
System.out.println( "In class method" );
return value + 10;
}
}
public class TestStaticFields {
public static void main( String args[] ) {
System.out.println( "Start Test" );
StaticFields test = new StaticFields ( );
StaticFields secondObject = new StaticFields ( );
StaticFields.size = 100;
System.out.println( test.size ); // Print 100
}
}
Start Test In class method 30 100
public class Top {
public static void main( String[] arguments ) {
System.out.println( "In top main" );
Bottom.main( arguments );
}
public static void print() {
System.out.println( "In top" );
}
}
public class Bottom {
public static void main( String[] arguments ) {
System.out.println( "In bottom main" );
Top.print( );
OddMain.main( arguments );
}
}
public class OddMain {
public static int main( String[] arguments ) {
System.out.println( "In odd main" );
Top hat = new Top();
hat.print( );
return 5;
}
}
public class Constants
{
protected final int SIZE = 10;
protected final int[] CLASS_ARRAY = new int [ SIZE ];
protected final int NO_VALUE_YET; // blank final
public void aMethod( int input, final float FIXED)
{
final int NEW_FEATURE = 123;
final int ALSO_FIXED = input;
CLASS_ARRAY[ 3 ] = input;
}
public Constants( int aValue )
{
NO_VALUE_YET = aValue;
}
public static void main( String args[] )
{
Constants test = new Constants( 5);
test.aMethod( 13, 2.2f);
System.out.println( test.NO_VALUE_YET ); // Prints 5
}
}
public class StaticConstants
{
protected static final int SIZE;
static
{
SIZE = 123;
}
}
public class Constants
{
protected final int SIZE;
{
SIZE = 123;
}
}
public class Constants
{
protected final int SIZE;
public Constants()
{
this( 5 );
SIZE = 123;
}
public Constants( int newSize )
{
SIZE = newSize;
}
public static void main( String args[] )
{
Constants whichOne = new Constants();
System.out.println( whichOne.SIZE );
}
}
public class Test
{
public static void main( String args[] )
{
final Sample object = new Sample();
object.data = 5; // OK
object.setData( 12 ); // OK
object = new Sample();“// Compile Error
}
}
public class Sample
{
public int data = 3;
public void setData( int newValue )
{
data = newValue;
}
}
public class ForwardReferenceAndInitialization
{
public static int first = 1;
public static int second = first * 2;
public static int third = fourth - 1; // Compiler error
public static int fourth = 4;
public int fifth = 5;
public int sixth = fifth + 1;
public int seventh = eighth - 1; // Compiler error
public int eighth = 8;
}
When initializing a field you can make a forward reference to a method
public class ForwardReferenceAndFunctions
{
public int fifth = getSixth();
public int sixth = fifth + 1;
public int seventh = getSixth();
public int getSixth()
{
return sixth;
}
}
public class Test
{
public static void main( String[] arguments )
{
ForwardReferenceAndFunctions works;
works = new ForwardReferenceAndFunctions();
System.out.println( "fifth " + works.fifth );
System.out.println( "sixth " + works.sixth );
System.out.println( "seventh " + works.seventh );
}
}
fifth 0 sixth 1 seventh 1
public class ArrayExample
{
public static void main( String[] args )
{
int[] aReference;
aReference[ 2 ] = 12; //Compile error
aReference = new int[ 5];
System.out.println( aReference[ 3 ] );
}
}