Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.

Similar presentations

Presentation on theme: "Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many."— Presentation transcript:

1 Programming in Java Unit 2

2 Class and variable declaration A class is best thought of as a template from which objects are created. You can create many instances of a class. Creating a class is referred to as ‘instantiating’. A Java class uses variables to define data fields and methods to define actions. A class method/s known as constructors are designed to perform initialization of data fields of objects, and can also perform any other actions.


4 This program begins with // in line 1, indicating comments in a line. Line 2 indicates the program file is in a package called welcome In line 3, public class Welcome is a programmer defined class Class is a keyword(reserverd by Java) that is used as a prefix to a class name In line 5, public static void main (String []args) indicates the main method which is void thus it returns nothing. In line 7, System.out.println (“Welcome to Java 2015”) instructs the computer to perform an action thus it will print a string of characters that are within the double quotes.

5 State (instance variables) Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an instance variables make up the object's state. You must always refer to it with the object that you declared, as shown in the next example: ClassName myObj = new ClassName(); myObj.theVariable = 90;

6 Behaviour (methods) Methods (called functions or procedures in other languages) actually assist you in modularizing a program into self-contained manageable units. The main method is called once to begin program execution. When programmers create a class, they create methods for that class. Methods are where the class' logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.

7 Constants and variables Variables should start with a lowercase letter, short, meaningful names, which sound good to us as programmers. Some examples:  buttonWidth  accountBalance  myString A constant variable is a variable that contains a value which does not change for the entire program, thus it should be declared with the keyword final. Constants should be named using uppercase letters with underscore characters as separators. private final int INCREMENT //Declares a constant called INCREMENT of type int that cannot be modified

8 //Declaring class called Dog public class Dog { //Declaring variables name and colour of type string String name; String colour; public void eat() { //method called eat //Eat code in here } public void bark() { //Bark code in here } }

9 Access modifiers A class can consist of modifiers as well. Access modifiers determine the visibility or accessibility of an object’s attributes and methods to other objects. Before we can begin implementing our design, we must consider which attributes and methods of our classes should be public and which should be private. A modifier is a keyword that is added in front of the class and is used to describe how the class can be accessed or used. Variables or methods declared with access modifier private are accessible only to the methods of the class to which they are declared.

10 Public (class) modifier A public class may be accessed from anywhere. Classes that are not public can only be accessed from other classes that are within the same directory. public class Dog { //Class body } When a class is declared public, it is visible to all other classes. Take note that you may only have one public class in each file. The name of the file must also be the same as the name of the public class. When using the access modifier public, it means that that classes, methods and data fields can be accessed from other classes.

11 Final (class) modifier A final class may not be extended. The compiler will return an error if you try to extend a class with a final modifier. final class Dog { //Class body }

12 Abstract (class) modifier An abstract class is used to declare one or more abstract methods. A class cannot be both final and abstract. It can be public and abstract, though. abstract class Dog { //Class body }

13 Default (class) modifier When you do not specify an access modifier for a class, the class will have default/package access. This means that the class will only be visible, and can only be used, in the current package or directory.

14 Public (member) modifier Public access for members of a class works in the same way as it does with classes. The members are visible to, and can be used by, any other class.

15 Default (member) modifier Members with default access are only visible to other classes in the same package. This happens when there is no access modifier declared, then by default the classes, method and data fields are accessible by any class in the same package. This is known as package-access.

16 Protected (member) modifier Protected members are the same as default members, but are also visible to subclasses of a class that can be in any other package.

17 Private (member) modifier Private members are only visible to the class and cannot be used by any other class. A private data field cannot be accessed by an object from outside the class that defines the private field. To make a private data field accessible, provide a get method to return its value and provide a set method to set a new private data field value.


19 The this reference Every object can access a reference to itself by using the keyword this also known as the this reference. The this keyword is the name of a reference that refers to a calling object itself. Its common uses are to reference a class's hidden data fields. A hidden instance variable can be accessed by using the keyword this.

20 Set and Get methods Set methods are also known as mutator methods as they change the object’s state. This means that they modify values of instance variables. Get methods are also known as accessor/query methods.

21 Composition A class can have reference to objects of other classes as members. This is called ‘composition’ and is usually termed the has-a relationship.

22 Garbage collection through the method finalize The method finalize is a method of class object under package java.lang. The JVM performs automatic garbage collection to reclaim memory occupied by redundant objects. This means that when there are no more references to an object it is eligible to be collected. The finalize method is called by the garbage collector to terminate an object just before it reclaims memory. This method finalize has no argument list and always has return type void.

23 Static class members The declaration begins with keyword static. This is normally used when every object has its own copy of all the instance variables of the class, therefore one copy of a variable will be shared by all objects of a class thus we make use of static variables to represent class wide information. A static cannot access non-static class members, because a static method can be called even when no objects of the class have been instantiated, thus the this reference cannot be used in a static method.

24 Creating packages Packages are used to ease complexity and be able as programmers to manage application components. They also aid in software reuse as well as unique naming convention. This basically means that each class in the Java API belongs to a package that contains a cluster of related classes.

25 Constructors Each class you declare can provide a special method called constructor that can be used to initialise an object of a class when the object is created. A constructor must have the same name as the class itself (defining class). Constructors do not have a return type - not even void. Constructors are invoked using the new operator when an object is created, which plays the role of object initialisation Java requires a constructor call for every object that is created. A constructor must have the same name as the class and it uses the keyword new which requests memory to store the object, and then calls the corresponding class’s constructor to initialise the object. A constructor without arguments is referred to as a no-argument constructor. A class that is defined without a constructor calls a no-argument constructor by default.

26 Overloaded constructors This enables objects of a class to be initialised in different ways. To achieve this one should simply provide multiple constructors with different signatures or parameter/argument list.

27 Method overloading Methods of the same name can be declared in the same class having different set of arguments or parameters. When an overloaded method is called, the Java compiler selects the appropriate method based on the arguments and their signature.


29 Method overriding When you define a method in your subclass that has exactly the same signature as a method in the superclass, the method is overridden and replaces the inherited method. When overriding methods, you are not allowed to make the methods more private, for example, you cannot define a public method as protected.


31 Relational operators High-level programming like Java provides selection statements that allow choice of action with alternative courses. Java selection statements use conditions, which are Boolean in expressions. This means that conditions are expressions that can be true or false, thus a computer program should be able to make a decision based on a condition’s value.

32 ==

33 Test your knowledge Complete Self Review Exercises from your e-book, p. 67 - 74. Complete exercises from your Toolbox  Research exercise: 2, 3  Group work: 1, 2, 5, 10  Tutorial: 2, 3  Lab exercises: 1, 2, 3, 7

Download ppt "Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many."

Similar presentations

Ads by Google