Java3 - COP 3503 Computer Science II Java Notes#3 More Details on Specifiers and an Introduction to Recursion in Java Introduction In the previous

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction In the previous two sections of notes on Java you were introduced to the basics of classes and methods in the Java language. In the second set of notes special attention was given to access specifiers and method overloading. In this set of notes the emphasis is on some additional features of the access specifiers as well as a look at the nesting of classes. We’ll also take a quick look at recursion in Java. The Static Modifier Sometimes you will need to define a member of a class that will be used independently of any object in that class. Normally, a class member must be accessed through an object of its class, but it is possible to create a member than can be used by itself, without reference to a specific instance. To create such a member, its declaration is preceded with the keyword static . When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. Both methods and variables can be declared as static. The most common example of a static member is main( ) . The main( ) method is declared static because it must be called by the operating system when your program begins and thus is called before any objects have been instantiated. Outside of the class is which it is declared, a static member can be used simply by specifying the name of its class followed by the dot operator. No object needs to be created to access the static member. For example, if you want to assign the value of 8 to a static variable called count that is part of the Timer class, you simply use the code: Timer.count = 8 ; Notice that this format is similar to that used to access normal instance variables through an object, except in this case the class name is used. A static method can be called in exactly the same way – through the use of the dot operator on the name of the class. Variables that are declared as static are essentially global variables. When an object is declared, no copy of a static variable is created. Instead, all instances of the class will share the same static variable. Shown below is an example COP 3503 – Java Notes #3 - 1 COP 3503 – Computer Science II – Java Notes #3 More Details on Specifiers and an Introduction to Recursion in Java
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
that illustrates the differences between a static variable and an instance variable. The output from the execution of this program is: The next example illustrates a static method. COP 3503 – Java Notes #3 - 2 //Program to illustrate the differences in static variables and instance variables class StaticDemo { int x; //this is a normal instance variable static int y; //this is a static variable } class SVDemo { public static void main(String args[ ]) { StaticDemo obj1 = new StaticDemo( ); StaticDemo obj2 = new StaticDemo( ); //each object has its own copy of an instance variable – in this case the variable x obj1.x = 2; obj2.x = 4; System.out.println(“obj1.x and obj2.x are independent – they are instance variables”);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 8

Java3 - COP 3503 Computer Science II Java Notes#3 More Details on Specifiers and an Introduction to Recursion in Java Introduction In the previous

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online