Docsity

Java Programming: Understanding Class Variables and this Keyword in CSIS 110 - Prof. Brian, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 โ€“ Lecture 28
Reading -
Quiz โ€“ Monday, March 28
Last time we looked at:
- Passing reference types to methods. Look at PassingReferences2.java, which
creates JFrames.
- Instance methods and the this keyword. Look at Point.java
All types are passed by value - it does not matter if the type of the formal parameter is a
primitive type or a reference type. This means that if we change the value of a formal
parameter, that change is not reflected back in the calling method.
Repeat KEY POINT:
If we change the value that is referenced by a reference type, that change is reflected
back in the caller. This is a subtle but important distinction!
Keyword this
Let's look at a class that defines a point on a two-dimensional grid - see Point.java. Look
at the constructor that takes two arguments
public Point( double xValue, double yValue )
I'd really like to give the formal parameters the names x and y instead of having to make
up new names for the same thing - points really have an x value and a y value. So, I'd like
to declare Point like this:
public Point( double x, double y )
What happens if I do it like this?
However, Java provides a way for us to do what I want - the keyword this. 'this' is a
reference to the current object. (Try to draw a picture). So, we can modify our code to use
this.
Class Variables
OK, we've seen instance methods, instance variables, and class methods. There is also
something called a class variable
Like a class method, a class variable is created using the keyword static.
[public/private] static <type> <identifier> = <initialization> ;
pf3

Partial preview of the text

Download Java Programming: Understanding Class Variables and this Keyword in CSIS 110 - Prof. Brian and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 โ€“ Lecture 28

Reading - Quiz โ€“ Monday, March 28 Last time we looked at:

  • Passing reference types to methods. Look at PassingReferences2.java, which creates JFrames.
  • Instance methods and the this keyword. Look at Point.java All types are passed by value - it does not matter if the type of the formal parameter is a primitive type or a reference type. This means that if we change the value of a formal parameter, that change is not reflected back in the calling method. Repeat KEY POINT: If we change the value that is referenced by a reference type, that change is reflected back in the caller. This is a subtle but important distinction! Keyword this Let's look at a class that defines a point on a two-dimensional grid - see Point.java. Look at the constructor that takes two arguments public Point( double xValue, double yValue ) I'd really like to give the formal parameters the names x and y instead of having to make up new names for the same thing - points really have an x value and a y value. So, I'd like to declare Point like this: public Point( double x, double y ) What happens if I do it like this? However, Java provides a way for us to do what I want - the keyword this. 'this' is a reference to the current object. (Try to draw a picture). So, we can modify our code to use this. Class Variables OK, we've seen instance methods, instance variables, and class methods. There is also something called a class variable Like a class method, a class variable is created using the keyword static. [public/private] static = ;

A class variable is independent of the objects in the class. There is only one instance of a class variable, regardless of how many objects exist. Math.PI is a defined as a class variable (actually a constant because it's final). Let's look at a class that includes a class variable - Counter.java The methods reset(), getValue(), and click() should all be pretty familiar to you - they are instance methods. Method getHowMany() is a class method - note the keyword static The variable howMany is a class variable - again, note the keyword static. howMany gets initialized to 0, and is incremented everytime the Counter constructor is invoked. [Draw picture] Scope We've seen that variables can only be used in certain parts of a program

  • formal parameters: in the method that declares them
  • for loop control variables declared in the loop initializer - the for loop only This is called the scope of a variable. The scope of a variable is the set of statements in which the variable is available for use. The scope of a variable (or constant) depends on what type of variable it is:
  • local variable (or constant) : scope is the set of statements ranging from the variable declaration to the end of the enclosing block.
  • class and instance variable (or constant) : scope is the entire class
  • for loop control variable (special case of local variable) : scope is the entire loop
  • formal parameter : scope is all statements in the method Let's look at some examples using Point, Demo3, etc. Name Reuse and Hiding We've seen many situations where we have reused a variable name. I might have a local variable x in main, an instance variable x in a class, and a formal parameter x. When can I use the same name for a variable? Instance and class variables - can only use a name once within a particular class. Can reuse the name in other classes