Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming: Understanding Classes, Variables, and Methods, Slides of Object Oriented Programming

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.

Typology: Slides

2012/2013

Uploaded on 04/24/2013

ballari
ballari 🇮🇳

4.6

(9)

118 documents

1 / 5

Toggle sidebar

Related documents


Partial preview of the text

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

3

Basics of Classes (cont)

  • Initialization blocks are used to initialize large

number of variables like the elements of a large

array. Ex. TryInitialization (try non static)

  • Constructor: Mainly for guaranteeing that class

variables are properly initialized. Ex. Sphere &

CreateSphere (Compile & run)

  • Method overloading and multiple constructors

(CreateShper2 directory)

  • Using objects (Point.java, Line.java, &

TryGeometry.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.
  • Standard Java packages. See documentation

5

Access Attributes & Nested Classes

  • There are four attributes: friendly, public,

private, and protected

  • Ex: 251-Point, 252-Line, & 254-TryPackage
  • Java nested classes & static nested classes.

Ex: MagicHat & TryNesstedClasses

  • Using Non-static nested classes. Ex: MagicHat
  • The finalize() method
  • You can declare native methods using the

“native” keyword but you lose portability.