An introduction to classes in java programming, including the basics of objects, fields, methods, instance and class variables, accessing variables and methods, static methods, initialization blocks, constructors, method overloading, java packages, and accessing attributes and nested classes.
Download Java Programming: Understanding Classes, Variables, and Methods and more Slides Object Oriented Programming in PDF only on Docsity!
1
Chapter 5: Defining Classes
2
Basics of Classes
An object is a member of a class type
What is a class? Fields & Methods
Types of variables:
Instance: each instance (object) of the class will have its own copy of instance variables
Class: Only one copy is shared by all objects
Types of methods: Instance & class (static) method
Accessing variables and methods (dot operator)
Accessing static variables and methods?
Methods: return type, name, & parameters
Accessing class Data members in methods
Example: Sphere.java
4
JAVA Packages
Recursion means a method can call itself!! A behavior that can be very useful. Ex: 234-PowerCalc.java
A Java package is basically a group of Java classes stored in a directory that has the exact name as the package name
To create a package use the “package ” as the first statement in all the classes of that package
To compile a class in a package, the path to that package should be defined in your “classpath” variable or use – classpath compile option
To run a class in a package, you need to run JVM from the upper directory containing that package and to specify the name of the package & the class name separates by a period.