You are on page 1of 2

Starting out with Java

Chapter 2: Java Fundamentals


// comment

A java program must have one class

The class definition contains three words


Public class Simple
Public = an access modifier, it controls where the class can be accessed from. Public means the access to
the class is unrestricted
Class = is a java keyword that indicates the beginning of a class
Simple = is a name of the class made up by the programmer

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

Public static void (string[] args)

Page 32

How to get user import


Import java.util.scanner;
Declare Scanner class
.nextInt
.nextLine
.nextDouble

Page 88: mixing NextLine with Calls to Other Scanners

Comparing String Objects page 145


== vs. name1.equals(“mark”)

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

Constructors (page 348)


-HAS THE SAME NAME AS THE CLASS
-IT DOES NOT HAVE A RETURN TYPE (even though it is a method)
-not even void
-this is because constructors are not executed by explicit method calls and cannot return a value

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

Overloading Methods and Constructors


-multiple methods in the same class have the same name, but use DIFFERENT TYPES OF PARAMETERS
-constructors can be overloaded

Shadowing Local Variables

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

The This Reference Variables (page 531)


-this key word is the name of a reference variable that an object can use to refer to itself. It is available
to all non-static methods
-using this to overcome shadowing
-using this to call an overloaded constructor from another constructor

You might also like