1. Defining Classes in JAVA
Mehdi Ali Soltani
mehdi.alisoltani@gmail.com
https://www.linkedin.com/in/mehdi-ali-soltani-315453b7/
2. WHAT IS A CLASS?
A class is a prescription for a particular kind of object.
3. Class Properties
➢ Fields: These are variables that store data items that typically differentiate
one object of the class from another. They are also referred to as data
members of a class.
➢ Methods: These define the operations you can perform for the class so they
determine what you can do to, or with, objects of the class. Methods typically
operate on the fields the data members of the class.
4. Static and non Static Fields
➢ Non-static fields, also called instance variables :
Each object of the class has its own copy of each of the non-static fields or
instance variables that appear in the class definition.
➢ Static fields, also called class variables
A given class has only one copy of each of its static fields or class variables,
and these are shared between and among all the objects of the class
6. ➢ instance methods
instance methods can be executed only in relation to a particular object
MyObject obj = new MyObject();//Create an instance
obj.nonstaticMethod();//Refer to the instance's class's code
➢ class methods
Class method can be executed even when no objects of a class exist
e.g main(), Math.sqrt(), ...
Methods in a Class Definition
7. Accessing Variables and Methods
➢ static method
double rootPi = Math.sqrt(Math.PI);
➢ Non static method
double ballVolume = ball.volume();
8. Final Fields
final fields means that the field cannot be modified by the methods in the class
final double PI = 3.14;
You should provide an initial value for a final field when you declare it.
11. Returning from a Method
To return a value from a method when its execution is complete use a return
statement.
return return_value;
12. The Parameter List
Parameter and an argument:
➢ A parameter has a name and a type and appears in the parameter list in the
definition of a method. A parameter defines the type of value that can be
passed to the method when it is called.
➢ An argument is a value that is passed to a method when it is executed, and
the value of the argument is referenced by the parameter name during
execution of the method
14. How Argument Values are Passed to a Method
All argument values are transferred to a method using what is called the
pass-by-value mechanism
15. A method can change an object that is passed as an argument. This is because a
variable of a class type contains a reference to an object, not the object itself.
Thus, when you use a variable of a class type as an argument to a method, a copy
of a reference to the object is passed to the method, not a copy of the object
itself. Because a copy of a reference still refers to the same object, the
parameter name used in the body of a method refers to the original object that
was passed as the argument.
Passing Objects to Methods
16. Final Parameters
Specifying any of the parameters for a method as final. This has the effect of
preventing modification of any argument value that is substituted for the
parameter when you call the method.
18. Defining Class Methods(cont.)
Remember that you cannot directly refer to any of the instance variables in the
class within a static method. This is because a static method can be executed
when no objects of the class have been created, and therefore no instance
variables exist.
20. Variable this
Every instance method has a variable with the name this that refers to the current
object for which the method is being called. The compiler uses this implicitly
when your method refers to an instance variable of the class
return 4.0/3.0*PI* this.radius *this.radius*this.radius;
21. Sources of Data Available within a Method
➢ Arguments passed to the method, which you refer to by using the parameter
names
➢ Data members, both instance variables and class variables, which you refer
to by their names
➢ Local variables that you declare in the body of the method
➢ Values that are returned by other methods that are called from within the
method
23. CONSTRUCTORS
When you create an object of a class, a special kind of method called a
constructor is always invoked
If you don't define any constructors for your class, the compiler supplies a
default constructor in the class, which does nothing
24. Constructor Characteristics
➢ A constructor never returns a value, and you must not specify a return type
not even of type void
➢ A constructor always has the same name as the class.
27. Passing Objects to a Method
When you pass an object as an argument to a method, the mechanism that
applies is called pass-by-reference, because a copy of the reference contained in
the variable is transferred to the method, not a copy of the object itself.
29. The Lifetime of an Object
The lifetime of an object is determined by the variable that holds the reference
you can reset a variable to refer to nothing by setting its value to null.
ball = null;
the variable ball no longer refers to an object. The lifetime of an object is
determined by whether any variable anywhere in the program still references it.
The process of disposing of dead objects is called garbage collection
31. METHOD OVERLOADING
Java enables you to define several methods in a class with the same name, as
long as each method has a unique set of parameters. Defining two or more
methods with the same name in a class is called method overloading
Different ways to overload the method
1. By changing number of arguments
2. By changing the data type
35. Calling a Constructor from a Constructor
One class constructor can call another constructor in the same class in its first
executable statement.
To refer to another constructor in the same class, use this as the method name,
followed by the appropriate arguments between parentheses
37. Duplicating Objects Using a Constructor
Sphere eightBall = new Sphere(10.0, 10.0, 0.0);
Later in your program you want to create a new object newBall, which is identical
to the object eightBall.
If you write
Sphere newBall = eightBall;
variable newBall references the same object as eightBall. You don't have a
distinct object
39. UNDERSTANDING PACKAGES
package is a namespace that organizes a set of related classes and interfaces.
Conceptually you can think of packages as being similar to different folders on
your computer.
41. Adding Classes from a Package to Your Program
Import by using the import keyword:
import package.myclass;
42. CONTROLLING ACCESS TO CLASS MEMBERS
Provide you you control the accessibility of class members from outside the class.
Using Access Attributes
ATTRIBUTE PERMITTED ACCESS
No access attribute From methods in any class in the same package.
public From methods in any class anywhere as long as the
class has been declared as public.
private Accessible only from methods inside the class. No
access from outside the class at all.
protected From methods in any class in the same package
and from any subclass anywhere.
46. Choosing Access Attributes
variables in a public class should be private and the methods that are called from
outside the class should be public.
➢ For classes in a package that are not public, and therefore not accessible outside the package, it
may sometimes be convenient to allow other classes in the package direct access to the data
members.
➢ If you have data members that have been specified as final so that their values are fi xed and they
are likely to be useful outside the class, you might as well declare them to be public.
➢ You may well have methods in a class that are intended to be used only internally by other
methods in the same class. In this case you should specify these as private.
➢ In a class like the standard class Math, which is just a convenient container for utility functions
and standard data values, you should make everything public.
47. NESTED CLASSES
All the classes you have defined so far have been separate from each other —
each stored away in its own source file. You can put the definition of one class
inside the definition of another class. The inside class is called a nested class. A
nested class can itself have another class nested inside it, if need be.