SDSU CS 596 Client-Server Programming
Java Class

[To Lecture Notes Index]
San Diego State University -- This page last updated January 31, 1996
----------

Contents of Java Class Lecture

  1. Classes
    1. Basic Terms
    2. Fields (Class Members)
    3. Access Levels - Variables and Methods
      1. Public, Protected, Private
    4. Object Variables are References
    5. Parameter Passing - By value only
    6. Class Fields - Static
    7. Constants

Classes


Basic Terms

Methodfunction member of class
Variablesdata member of class
FieldVariables and methods of a class
Keywords to Cover

public, protected, private

static, final, finalize

abstract, interface, extends, implements

this, super

import, package

synchronized
Comparisons to C++

Similar class syntax and structure

No multiple inheritance, uses interfaces instead

Functions are virtual by default

Constructors are much simpler

Destructors are not need

Packages provide separate name spaces for classes

Fields (Class Members)

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  );

	}
}
Output

Start main
In sample
10
Running Above Program

Put classes FieldExample and CreateObject in file FieldExample.java

rohan 50-> ls
FieldExample.java

rohan 51-> javac FieldExample.java

rohan 52-> ls
CreateObject.class FieldExample.class FieldExample.java

rohan 53-> java CreateObject
Start main
In sample

rohan 54->
Note
Multi-File Programs

Put class FieldExample in file FieldExample.java

Put class CreateObject in file CreateObject.java

Compile top level class - javac will compile needed classes

Example

rohan 13-> ls
CreateObject.java FieldExample.java

rohan 14-> javac CreateObject.java

rohan 15-> ls
CreateObject.class FieldExample.class
CreateObject.java FieldExample.java

rohan 16-> java CreateObject
Start main
In sample

rohan 17->

Access Levels - Variables and Methods


public
Accessible anywhere class name is accessible


protected
Accessible in the package that contains the class
Accessible in all subclasses


private
Accessible in the class only


If no access level is given
Accessible in the package that contains the class
Accessible in subclasses defined in the package that contains the class
Not accessible in subclasses defined outside the package that contains the class

Public, Protected, Private

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
	}
}

Object Variables are References

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 );
	}
}

Output
C

Parameter Passing - By value only


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  );
	};
};

Output
C

How to get Functions to return values

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';
	}; 
};
Output
B
How to get Functions to return values
Using References - Array

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';
	};
};
Output
A
How to get Functions to return values
Using References - Class

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 );
	};
};
Output
A

Class Fields - Static


Static
Declares variables (methods) to be class variables (methods)
Same as C++ static class members
Class Fields - Static
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;
	}
}

Constants


final variables are constants

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
	}
}

----------