{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

java3 - COP 3503 Computer Science II Java Notes#3 More...

Info icon This 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
Image of page 1

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

View Full Document Right 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”); System.out.println(“obj1.x is: “ + obj1.x +” obj2x is:” + obj2.x);
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern