Starting Out With Java-Gaddis Notes
Starting Out With Java-Gaddis Notes
Rules
-you can create more than one class in a file, but you may only have ONE PUBLIC CLASS per java file
-when a java file has a public class, the name of the public class must be the same name as the name of
the file
-for sample public class simple, should be stored as Simple.java
-everything in the class is within the braces {}
-every java application must have a main method
Page 32
CLASSES
Private vs. public access modifiers
-private – member can only be access within inside the class
-public – member can be access inside or outside the class
It’s common practice to make all fields private and provide access to the fields through methods
Methods
-will have both an access modifier and a return type
-static vs. non-static is optional
-when you don’t write static, the method is called an instance method
Default Constructor
-what if we do not write a constructor in the object’s class?
-if you do not write a constructor in a class, Java automatically provides one when the class is complied
-the default constructor does not accept any arguments
-it sets all object’s numeric fields to zero and Boolean fields to false
Static
-a static class member belongs to the class, not objects instantiated from the class
-static fields and static methods
-static methods do not operate on fields that belong to any instance of the class. Instead, they can only
operate on static fields
Static Methods
-it isn’t necessary for an instance of the class to be created in order to execute the method
-the only limitation that static methods have is that they cannot refer to non-static members of the class