SlideShare ist ein Scribd-Unternehmen logo
1 von 14
Life and Death of an Object LIS4930 © PIC As we learned you are responsible for constructing objects that live on the heap, and we know the heap is memory, so objects must take up memory. So why don’t we run out of memory? Heap Enter the Java Garbage Collector (gc)
Stacks and Heaps LIS4930 © PIC Before we can talk about object construction we need to first talk about where everything lives (and for how long) in Java. In Java there are two areas of memory: one we already know is the Heap and we know that is where objects live, but where does everything else live, like methods? YIP! On the stack. STACK HEAP addToArray( ) shout( ) go( ) bike Dog Boy doStuff( ) main( )
Where do Variables live? Instance Variables belong to Objects so they are part of the Object living on the HEAP Local Variables belong to Methods so they are part of the Method living on the STACK LIS4930 © PIC BUT what if a local variable is an object? A local variable is not an object just a reference to one! public class Stacker{     public void foof(){       barf();    }    public void barf(){        Duck d = new Duck(24);    } } Duck barf( ){         } foof( ) STACK HEAP
Methods are Stacked! LIS4930 © PIC When you call a method, the method lands on the top of a call stack. That new thing that’s pushed onto the stack is the stack frame. The stack frame remains on the stack until the closing curly brace ‘ } ‘ is reached in the method. Then the stack is removed. STACK The method on the top of the stack is always the currently executing method. doStuff( ) Look at the Stack Scenario on page 237. main( )
The Miracle of Object Creation LIS4930 © PIC Let’s review how to create an object. Declare a reference variable 1 Dog rufus= new Dog( ); Dog rufus = new Dog( ); Dog rufus=new Dog( ); Create an object 2 Link the object and the reference 3
LIS4930 © PIC The Miracle of Object Creation Dog rufus = new Dog( ); NOPE! We’re calling the Dog constructor. Are we calling a method named Dog()? Because it sure looks like it. A constructor does look and feel a lot like a method, but it’s not a method. It’s got the code that runs when you say new. In other words, the code that runs when you instantiate an object. The only way to invoke a constructor is with the keyword new followed by the class name.
Who Constructed the Constructor? LIS4930 © PIC You can write a constructor for your class, but if you don’t Java will make one up for you, and it is called the default constructor. It looks like this: public  Dog() { //constructor code goes here } Notice anything missing?
Constructor Example LIS4930 © PIC public class Duck { 	public Duck( ) { System.out.println(“Quack!”); 	} } A constructor lets you jump into the middle of the object creation step – into the middle of new. public class UseADuck { 	Duck Todd = new Duck(); } But what if we don’t know what the size of the should be? Most people use constructors to initialize the state of an object. In other words, to make and assign values to the object’s instance variables. public Duck( ) { 	size = 34; }
Old vs. New LIS4930 © PIC Old Approach New Approach
Make it Easy to Make a Duck LIS4930 © PIC Remember as soon as you create a constructor you DO NOTget given one from the JVM. Therefore, if we create a constructor that takes a parameter, you will always have to pass a parameter… Do you see an issue with this?  What if the programmer doesn’t know the size of the Duck at the time of creation? What we need is to keep the default constructor AND the new constructor… In other words we need to overload our default constructor.
Overloaded Constructor LIS4930 © PIC
Copy Constructors See in-class example. LIS4930 © PIC
Notes About Constructors If you create a constructor of any type you DO NOT get the default constructor from the JVM! If you write a constructor that takes arguments, and you still want a no-arg constructor, you’ll have to build the no-arg constructor yourself. If you have more than one constructor in a class, the constructor MUST have different argument lists. LIS4930 © PIC Look at the different constructor examples on page 248 in your textbooks.
Constructor Review LIS4930 © PIC A constructor is the code that runs when somebody says new on a class type: 1 Duck daffy = new Duck( ); public Duck ( int size) { } public Duck( ) { } public Duck( ) { } public Duck(int size) { } public Duck (String name, int size) { } A constructor must have the same name as the class, and  no return type: 2 If you don’t put a constructor in your class, the compiler puts in a default constructor. The default constructor is always a no-arg constructor: 3 You can have more than one constructor in your class, as long as the argument lists are different. Having more than one constructor in a class means you have  overloaded constructors. 4

Weitere ähnliche Inhalte

Andere mochten auch (6)

08 boolean expressions
08 boolean expressions08 boolean expressions
08 boolean expressions
 
14b exceptions
14b exceptions14b exceptions
14b exceptions
 
CH11 Graphics Notes
CH11 Graphics NotesCH11 Graphics Notes
CH11 Graphics Notes
 
01 intro to using java
01 intro to using java01 intro to using java
01 intro to using java
 
Chapter.08
Chapter.08Chapter.08
Chapter.08
 
Chapter.08
Chapter.08Chapter.08
Chapter.08
 

Ähnlich wie 12 constructors (20)

13 life and scope
13 life and scope13 life and scope
13 life and scope
 
13 interfaces
13 interfaces13 interfaces
13 interfaces
 
02 prepcode
02 prepcode02 prepcode
02 prepcode
 
Internet programming slide - java.ppt
Internet programming slide - java.pptInternet programming slide - java.ppt
Internet programming slide - java.ppt
 
Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
 
Twins: OOP and FP
Twins: OOP and FPTwins: OOP and FP
Twins: OOP and FP
 
04 variables
04 variables04 variables
04 variables
 
03 objects
03 objects03 objects
03 objects
 
The software design principles
The software design principlesThe software design principles
The software design principles
 
Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?
 
06a methods original
06a methods original06a methods original
06a methods original
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworks
 
Ontopia tutorial
Ontopia tutorialOntopia tutorial
Ontopia tutorial
 
Sour Pickles
Sour PicklesSour Pickles
Sour Pickles
 
iOS training (basic)
iOS training (basic)iOS training (basic)
iOS training (basic)
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Latest C++ Interview Questions and Answers
Latest C++ Interview Questions and AnswersLatest C++ Interview Questions and Answers
Latest C++ Interview Questions and Answers
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
 

Mehr von Program in Interdisciplinary Computing (20)

Phpmysqlcoding
PhpmysqlcodingPhpmysqlcoding
Phpmysqlcoding
 
Database basics
Database basicsDatabase basics
Database basics
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
01 intro tousingjava
01 intro tousingjava01 intro tousingjava
01 intro tousingjava
 
Web architecture v3
Web architecture v3Web architecture v3
Web architecture v3
 
Xhtml
XhtmlXhtml
Xhtml
 
Webdev
WebdevWebdev
Webdev
 
Web architecture
Web architectureWeb architecture
Web architecture
 
Sdlc
SdlcSdlc
Sdlc
 
Mysocial
MysocialMysocial
Mysocial
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Html5
Html5Html5
Html5
 
Frameworks
FrameworksFrameworks
Frameworks
 
Drupal
DrupalDrupal
Drupal
 
Database
DatabaseDatabase
Database
 
Javascript2
Javascript2Javascript2
Javascript2
 

12 constructors

  • 1. Life and Death of an Object LIS4930 © PIC As we learned you are responsible for constructing objects that live on the heap, and we know the heap is memory, so objects must take up memory. So why don’t we run out of memory? Heap Enter the Java Garbage Collector (gc)
  • 2. Stacks and Heaps LIS4930 © PIC Before we can talk about object construction we need to first talk about where everything lives (and for how long) in Java. In Java there are two areas of memory: one we already know is the Heap and we know that is where objects live, but where does everything else live, like methods? YIP! On the stack. STACK HEAP addToArray( ) shout( ) go( ) bike Dog Boy doStuff( ) main( )
  • 3. Where do Variables live? Instance Variables belong to Objects so they are part of the Object living on the HEAP Local Variables belong to Methods so they are part of the Method living on the STACK LIS4930 © PIC BUT what if a local variable is an object? A local variable is not an object just a reference to one! public class Stacker{ public void foof(){ barf(); } public void barf(){ Duck d = new Duck(24); } } Duck barf( ){ } foof( ) STACK HEAP
  • 4. Methods are Stacked! LIS4930 © PIC When you call a method, the method lands on the top of a call stack. That new thing that’s pushed onto the stack is the stack frame. The stack frame remains on the stack until the closing curly brace ‘ } ‘ is reached in the method. Then the stack is removed. STACK The method on the top of the stack is always the currently executing method. doStuff( ) Look at the Stack Scenario on page 237. main( )
  • 5. The Miracle of Object Creation LIS4930 © PIC Let’s review how to create an object. Declare a reference variable 1 Dog rufus= new Dog( ); Dog rufus = new Dog( ); Dog rufus=new Dog( ); Create an object 2 Link the object and the reference 3
  • 6. LIS4930 © PIC The Miracle of Object Creation Dog rufus = new Dog( ); NOPE! We’re calling the Dog constructor. Are we calling a method named Dog()? Because it sure looks like it. A constructor does look and feel a lot like a method, but it’s not a method. It’s got the code that runs when you say new. In other words, the code that runs when you instantiate an object. The only way to invoke a constructor is with the keyword new followed by the class name.
  • 7. Who Constructed the Constructor? LIS4930 © PIC You can write a constructor for your class, but if you don’t Java will make one up for you, and it is called the default constructor. It looks like this: public Dog() { //constructor code goes here } Notice anything missing?
  • 8. Constructor Example LIS4930 © PIC public class Duck { public Duck( ) { System.out.println(“Quack!”); } } A constructor lets you jump into the middle of the object creation step – into the middle of new. public class UseADuck { Duck Todd = new Duck(); } But what if we don’t know what the size of the should be? Most people use constructors to initialize the state of an object. In other words, to make and assign values to the object’s instance variables. public Duck( ) { size = 34; }
  • 9. Old vs. New LIS4930 © PIC Old Approach New Approach
  • 10. Make it Easy to Make a Duck LIS4930 © PIC Remember as soon as you create a constructor you DO NOTget given one from the JVM. Therefore, if we create a constructor that takes a parameter, you will always have to pass a parameter… Do you see an issue with this? What if the programmer doesn’t know the size of the Duck at the time of creation? What we need is to keep the default constructor AND the new constructor… In other words we need to overload our default constructor.
  • 12. Copy Constructors See in-class example. LIS4930 © PIC
  • 13. Notes About Constructors If you create a constructor of any type you DO NOT get the default constructor from the JVM! If you write a constructor that takes arguments, and you still want a no-arg constructor, you’ll have to build the no-arg constructor yourself. If you have more than one constructor in a class, the constructor MUST have different argument lists. LIS4930 © PIC Look at the different constructor examples on page 248 in your textbooks.
  • 14. Constructor Review LIS4930 © PIC A constructor is the code that runs when somebody says new on a class type: 1 Duck daffy = new Duck( ); public Duck ( int size) { } public Duck( ) { } public Duck( ) { } public Duck(int size) { } public Duck (String name, int size) { } A constructor must have the same name as the class, and no return type: 2 If you don’t put a constructor in your class, the compiler puts in a default constructor. The default constructor is always a no-arg constructor: 3 You can have more than one constructor in your class, as long as the argument lists are different. Having more than one constructor in a class means you have overloaded constructors. 4