CS62 - Spring 2021 - Class 3
Example code in this lecture
Initialization
Lecture notes
Admin
- my office hours today: 4-4:30pm
- how is assignment 1 going?
- assignment deadlines: 11:59 *any* time zone
- lab tomorrow
- quiz on Thursday
- assignment 2 out tomorrow
Quick recap from last time...
static
- variables
- Everything in Java has to be inside a class
- For instance variables, that means that each time we get a new copy of the object, we get extra copies of the variables
- For constants, that means each copy of the object will have it's own versions of the constants. Does that make sense?
- No!
- if we declare a variable to be static, there's only one version of it PER class, NOT per object
- all constants we will declare as static
- methods
- Java does not have functions, it only has methods
- a function is a standalone piece of code that is not associated with a class
- a method is called on an object, e.g. variable.methodName(...)
- There are some times when we might want to have function-like behavior? Any ideas when?
- Some things really should just be function (e.g., math operations like round, abs, etc. --
https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
)
- our code has to start somewhere. Chicken and the egg problem: what code constructs the first object?
- For these reasons, there are static methods
- details:
- static methods can be called *without* instantiated an object
- if you're inside the class, you can just write the name of the method (though this isn't that different from calling a non-static method)
- if you're outside of the class, you can call it by saying the class name . (dot) the method name
MyClass.staticMethodName(...)
- static methods cannot access any non-static instance variables (instance variables are associated with an instance of the class).
We're now able to understand the main method!!!
- public: accessible outside of the class
- static: stand-alone method that does not access/require any state of an object (i.e. access instance variables)
- void: doesn't return anything
- main: name of the method
- String[]: an array of Strings
- args: the name of the parameter
JavaDoc: documentation in Java
- JavaDoc is a way of commenting your code that *also* allows for html documentation to be generated!
-
http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html
- a way for you to communicate the "interface" to other programmers/users
- /** is the beginning comment delimiter for classes/methods
- if you type this above the class or a method and hit return, Eclipse will automatically generate a template for you!
- Two basic parts
- The document comment, which is written in HTML and is the first non-whitespace-line chunk of text
- block tags. The main ones we'll use are below, but many more online
- @author
- @param
- @return
- @throws
- We can generate the HTML from the comments using eclipse (or on the command line)
- In Eclipse: Project->Generate Javadoc...
- in the left window, select the classes that you want to generate the javadoc for (generally, you should do one project at a time)
- select the destination (the "doc" folder within the project is a good place)
- click "finish" (or "next" if you'd like other options)
- generate HTML output with the documentation!
initializing variables
- When we declare a new variable, Java creates a new space in memory for the value
- if it's a primitive type, the value will go directly there
- if it's anything else, it will be a reference to an object
- We often will declare the variable and assign to it immediately:
int x = 2;
Card c = new Card(1, "hearts");
- However, we can also decouple these steps:
int x;
x = 2;
Card c;
c = new Card(1, "hearts");
- What would happen if we looked at/printed out the value of a variable *before* assigning to it?
- In some languages, this isn't well defined!
- In Java, when you create a variable without assigning to it, it gets a default value
- For the primitive types, it's something reasonable (often 0)
- For everything else, we get "null"
- null is Javas way of indicating that this variable is not associated with a value.
look at Initialization.java in
Initialization code
- Anything unusual about the code?
- Where is the constructor?!?
- If you don't specify a constructor, Java will automatically generate a zero parameter constructor for you
- It doesn't do anything, but the object get created along with the instance variables
- this
- every class has an instance variable (that is implicitly declared) call "this"
- The type of the variable is the type of class, in this case Initialization
- It holds *the current object", i.e., it is a reference to the object that the methods are being called on
- Most of the time "this" isn't needed:
- you can just say
x = 2
- and don't need to say
this.x = 2
though they do the same thing
- in setX there is a parameter x and an instance variable x
- if we wrote x = x
- this is just assigning to the parameter x it's own value
- this.x = x
says store into the instance variable x the value of the parameter x
- we could have also just written
public void setX(int number){
x = number;
}
but it's often convenient to use the same name to make it explicit
look at Initialization.java in
Initialization code
- What would happen if we run test1()?
- We'll see:
2
4 of hearts
- What would happen if we run test2()?
- We haven't set the values, so we'll get the default values
0
null
- What would happen if we run test3()?
- try to call a method on an object that doesn't exist!
java.lang.NullPointerException
- You will get null pointer exceptions
- it just happens if you program long enough in Java
- The cause is that you tried to access some value as if it were an object, but it was actually null