COP 2800 Lake Sumter State College Mark Wilson, Instructor.

Slides:



Advertisements
Similar presentations
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Advertisements

Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Written by: Dr. JJ Shepherd
Constructors And Instantiation. Constructor Basics Every class must have a constructor Even abstract classes!! No return types Their names must exactly.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
AAA. Today’s lecture Review of Chapter 6 Go over examples.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Road Map Introduction to object oriented programming. Classes
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
OOP Languages: Java vs C++
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Programming in Java CSCI-2220 Object Oriented Programming.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Constructors & Garbage Collection Ch. 9 – Head First Java.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Chapter 9 Life & Death of an Object Stacks & Heaps; Constructors Garbage Collector (gc)
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
CLASS AND OBJECTS Valerie Chu, Ph.D. LeMoyne-Owen College 1.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
Lecture 11 Instructor: Craig Duckett Instance Variables.
Memory Management in Java Mr. Gerb Computer Science 4.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous.
Java Memory Management
Java Memory Management
Static data members Constructors and Destructors
Inheritance and Polymorphism
CSC 143 Inheritance.
CSC 113 Tutorial QUIZ I.
Overloading and Overriding
Classes & Objects: Examples
Inherited Classes in Java
Java Programming Language
Chapter 11 Inheritance and Polymorphism Part 1
Visibilities and Static-ness
Corresponds with Chapter 5
SPL – PS3 C++ Classes.
Chapter 5 Classes.
Presentation transcript:

COP 2800 Lake Sumter State College Mark Wilson, Instructor

Birth and Death of Objects

 The Stack All Methods Local variables First In Last Out queue  The Heap All Objects Instance variables Conceptual pile

Public static void main() { dostuff(); } Public void doStuff() { foo(); } Public void foo(){ bar(); } Public void bar(){ // code here }

 Java has two main areas of memory: Stack Heap  Instance variables are declared Inside a class but, Outside any method  Instance variables live on the heap  Local variables are declared Inside a method Inside a method parameter  Local variables live on the stack  Object reference variables work like primitive variables  Objects live on the heap regardless of where the reference variable lives

 Declare a reference variable Either class or interface type Thang aFooBar = new Thang();  Create an Object Object goes on the heap Thang aFooBar = new Thang();  Link object to reference variable Assign object to the reference Thang aFooBar = new Thang();

 Looks like a method call…  Thang aFooBar = new Thang();  But it’s not… It’s a constructor  Every class has a constructor  Supplied by the programmer  Supplied by the compiler  Runs before object is assigned to a reference variable  No return type  Must have the same name as the class public Thang (){ // there be constructor code here }

public class Thang { private int answerToLife; public Thang () { setAnswer(42); } public void setAnswer (int newAnswer) { answerToLife = newAnswer; } public int getAnswer () { return answerToLife; } public class doThang { public static void main (String[] args) { Thang aThang = new Thang(); System.out.println ( “The answer to life, the universe and everything” + aThang.getAnswer()); }

public class Thang { private int answerToLife; public Thang (int anAnswer) { setAnswer(anAnswer); } public void setAnswer (int newAnswer) { answerToLife = newAnswer; } public int getAnswer () { return answerToLife; } public class doThang { public static void main (String[] args) { Thang aThang = new Thang(42); System.out.println ( “The answer to life, the universe and everything” + aThang.getAnswer()); }

public class Thang { private int answerToLife; public Thang () { setAnswer(42); } public Thang (int anAnswer) { setAnswer(anAnswer); } public void setAnswer (int newAnswer) { answerToLife = newAnswer; } public int getAnswer () { return answerToLife; } public class doThang { public static void main (String[] args) { Thang aThang = new Thang(42); System.out.println ( “The answer to life, the universe and everything” + aThang.getAnswer()); }

 Constructor Runs when new is used on a class type Must have the same name as the class Can’t have a return type Initializes state May be overloaded  Instance variables are initialized 0/0.0/false/null by default

 Compiler adds a default (no arg) constructor  If you add a constructor, the compiler won’t add the default  Always add a default constructor and supply reasonable default values  Overloaded constructors means there is more than one  Overloaded constructors must have different argument lists  Two constructors can’t have the same argument list  Argument list includes order and types

 Objects inherit everything from its superclasses  Object gets space for all the inherited elements  All the constructors in an object’s inheritance tree must run when the object is created  Process of calling parent constructors is called constructor chaining

 super() puts the superclass constructor on the stack  super() must be the first call in the constructor  super() is added by the compiler if it isn’t there  super() is added to each overloaded constructor if it isn’t there  super() can include arguments to invoke overloaded constructors in the superclass

 this() invokes an overloaded constructor from another constructor  this() refers to the current object constructors  this() can only be called from a constructor  this() must be the first statement in a constructor  this() can’t be used in a constructor that calls super()

 Depends on the life of the reference variable Local variable only lives within the time the method is on the stack Instance variable lives as long as it’s object does  Life is not the same as scope  Object lives as long as there is a live reference to it  Unreferenced objects may continue to exist Can’t be accessed Candidate for garbage collection

 Reference goes out of scope permanently  Reference is assigned to another object  Reference is explicitly set to null

 Reference goes out of scope permanently  Reference is assigned to another object  Reference is explicitly set to null public class DuckPen{ public void foo () { bar(); } public void bar () { Duck d = new Duck(); }

 Reference goes out of scope permanently  Reference is assigned to another object  Reference is explicitly set to null public class DuckPen{ Duck d = new Duck(); public void foo () { d = new Duck(); }

 Reference goes out of scope permanently  Reference is assigned to another object  Reference is explicitly set to null public class DuckPen{ Duck d = new Duck(); public void foo () { d = null; }