Lecture 04 PART II JAVA
Lecture 04 PART II JAVA
Java Modifiers
access modifier, meaning that it is used to set the access level for classes, attributes, methods and constructors.
Access Modifiers
For classes, you can use either public or default: For attributes, methods and constructors, you
can use the one of the following:
Modifier Description
Modifier Description
public The code is accessible for all classes
public The class is accessible by any other
class private The code is only accessible within the
declared class
default The class is only accessible by classes
in the same package. This is used default The code is only accessible in the same
when you don't specify a modifier. package. This is used when you don't specify
a modifier.
Example Example
An example to demonstrate the differences
public class Main { between static and public methods:
final int x = 10;
final double PI = 3.14; public class Main {
public static void main(String[] args)
{ static void myStaticMethod() {
Main myObj = new Main(); System.out.println("Static methods can be called without
myObj.x = 50; creating objects"); }
myObj.PI = 25; // will generate an
error: cannot assign a value to a final public void myPublicMethod() {
variable System.out.println("Public methods must be called by creating
System.out.println(myObj.x); objects"); }
}
} public static void main(String[ ] args) {
myStaticMethod();
myPublicMethod();
Main myObj = new Main();
myObj.myPublicMethod();
} }
Abstract
An abstract method belongs to an abstract class, and it does not have a body. The body is provided by the subclass:
•Main.java
Main.java
•Second.java
public class Main
{ Second.java
class Second
{
int x = 5;
public static void main(String[]
args)
{
}
Main myObj = new Main();
System.out.println(myObj.x);
}
Accessing Modify override existing
Attributes Attributes values:
You can access attributes by creating You can also modify attribute
an object of the class, and by using values:
the dot syntax (.):
The following example will create an
Example
object of the Main class, with the
Example
name myObj. We use the x attribute on
Change the value of x to 25:
the object to print its value: Set the value of x to 40:
public class Main { int x = 10;
Example public class Main {
public static void main(String[] args)
Create an object called "myObj" int x;
and print the value of x: {
public static void main(String[]
public class Main args) Main myObj = new Main();
{ {
int x = 5; myObj.x = 25; // x is now 25
public static void main(String[] Main myObj = new Main();
args) System.out.println(myObj.x);
{ myObj.x = 40;
System.out.println(myObj.x); }
Main myObj = new Main(); The final keyword is useful when
System.out.println(myObj.x); } you want a variable to always store
} } the same value, like PI (3.14159...).
} }
Java Class Methods : methods are declared within a class, and that they are used to perform
certain actions:
Example
Create a method named myMethod() in
Main:
And Inside main, call myMethod():
Multiple Parameters
Example
public class Main { You can have as many parameters as you
static void myMethod(String fname) like:
{
System.out.println(fname + " Example
Refsnes"); public class Main {
}
public static void main(String[] static void myMethod(String fname, int age)
args) {
{ System.out.println(fname + " is " + age);
myMethod("Liam");
myMethod("Jenny"); }
myMethod("Anja"); } }
public static void main(String[] args)
{
myMethod("Liam", 5);
myMethod("Jenny", 8);
myMethod("Anja", 31);
}
}
Static vs. Public
You will often see Java programs that have either static or public attributes and methods.In the example above, we created
a static method, which means that it can be accessed without creating an object of the class, unlike public, which can only be
accessed by objects:
Example
An example to demonstrate the differences between static and public methods:
Example
Create a constructor:
// Create a Main class
public class Main Note that the constructor name must match the
{
int x;
class name, and it cannot have a return
public Main() { x = 5; type (like void).
public static void main(String[] args)
{
Also note that the constructor is called when the
Main myObj = new Main();
System.out.println(myObj.x); object is created.
} } // Outputs 5
All classes have constructors by default: if you do
not create a class constructor yourself, Java creates
one for you. However, then you are not able to set
initial values for object attributes.
Constructor Parameters
Constructors can also take parameters, which is used to initialize attributes.
The following example adds an int y parameter to the constructor. Inside the constructor we set x to y (x=y). When we call the
constructor, we pass a parameter to the constructor (5), which will set the value of x to 5:
Example Example
public class Main { public class Main {
int modelYear;
int x; String modelName;
public Main(int y) { public Main(int year, String name) {
x = y; modelYear = year;
} modelName = name; }
public static void main(String[]
args)
{ public static void main(String[] args) {
Main myObj = new Main(5); Main myCar = new Main(1969, "Mustang");
System.out.println(myObj.x); System.out.println(myCar.modelYear + " " +
} } myCar.modelName);
// Outputs 5 } } // Outputs 1969 Mustang
Java Inner Classes
In Java, it is also possible to nest classes (a class within a class). The purpose of nested classes is to group classes
that belong together, which makes your code more readable and maintainable. To access the inner class, create an
object of the outer class, and then create an object of the inner class:
Example
class OuterClass {
int x = 10;
class InnerClass {
int y = 5;
} }
Example
class OuterClass {
int x = 10;
private class InnerClass {
int y = 5;
} }
Example
class OuterClass {
int x = 10;
static class InnerClass {
int y = 5;
} }
System.out.println(myInner.y);
} } // Outputs 5
Note: just like static attributes and methods, a static inner class does not have access to members of the outer class.
Access Outer Class From Inner Class
One advantage of inner classes, is that they can access attributes and methods of the outer class:
Example
class OuterClass {
int x = 10;
class InnerClass {
return x; } } }
System.out.println(myInner.myInnerMethod());
} } // Outputs 10