Notes about objects
Constructors
- Constructors have same name as class
- Constructors do not return any value. Do not make them return int, String, ...
They automatically return an object of the type they're constructing.
- you can have multiple contructors if they have different parameters
So this Dog(String name) { }
is a constructor, but
void Dog(String name) { }
is not.
Static
"static" + variable
- made when class is created
- only one copy used by all objects of that class
- accessed by Classname.variable, eg. Math.PI (but can also be accessed by an objectname e.g. fido.legs)
"static" + method
- made when class is created
- only one copy used by all objects of that class
- accessed by Classname.method, eg. Math.random() or Math.sin(25)
- can only access static methods and variables
"static" + class
ONLY with an inner class. This means that you don't have to create an instance of the outer class
in order to access the inner class. Otherwise you do.
Final
"final" + variable
"final" + method
- You cannot override this method in a subclass.
"final" + class
- You cannot make a subclass of this class.
- Many, but not all, of the Java standard library classes are final. eg. String, System.
So this is illegal: class String2 extends String{}
Graphics classes are normally not final so that you can expand and modify them - e.g. JButton.
- All methods in a final class are also automatically final.
Access Modifiers
private variable or method
- This means that access is restricted to that class, ie. that .java file.
- Sub classes cannot access this variable or method
- inner classes CAN access private variables in the outerclass AND the outer class can access private variables in the inner class
Inner Class Example
class MainClass{
public static void main(String[] args) {
int z = InnerClass.aaa;
}
private static int bbb = 18;
static class InnerClass{
private static int aaa = 19;
InnerClass() {
bbb = 20;
}
}
}
"package private" variable or method or class
- if there is NO MODIFIER, it is called "package private", but you do not type this into the source code.
- This means that access is restricted to that package (ie. that folder)
- Sub classes cannot access this variable or method
protected variable or method
- same as package private, except that subclasses CAN access it
- We rarely use this access modifier as there is really no need for it.
The other ones cover everything that needs to be done.
public variable or method or class
- Any class in any package can access this data
Classes can only be public or nothing.
If you have a private class on its own as a top-level class, then you can't get access to it from anywhere!
Inner or Nested classes can be private
which means that access is restricted to the scope of that outer class.
public
The class that starts your program must be public and it must have a public method called main().
Actually, it has to be written specifically as public static void main(String[] args) {}
Abstract
"abstract" + class
- You cannot instantiate (make an object of that class)
- You have to make a subclass of this class in order to use it.
- If there are any abstract methods in a class, the whole class must be abstract
- Abstract classes are similar to interfaces, but there are differences. The link below explains this.
"abstract" + method
- I don't know what this means. I'm not sure why you'd have non-abstract methods in an abstract class.
-
Read more here