Suche senden
Hochladen
12 constructors
•
Als PPTX, PDF herunterladen
•
0 gefällt mir
•
428 views
P
Program in Interdisciplinary Computing
Folgen
Melden
Teilen
Melden
Teilen
1 von 14
Jetzt herunterladen
Empfohlen
Inheritance
Inheritance
Michal Píše
DEF CON 23 - Topher Timzen and Ryan Allen - hijacking arbitrary NET APP Contr...
DEF CON 23 - Topher Timzen and Ryan Allen - hijacking arbitrary NET APP Contr...
Felipe Prado
Object Detection with Tensorflow
Object Detection with Tensorflow
ElifTech
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
JSFestUA
constructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
06b extra methods
06b extra methods
Program in Interdisciplinary Computing
11 interfaces
11 interfaces
Program in Interdisciplinary Computing
CH 9: Editorial Style Notes
CH 9: Editorial Style Notes
Program in Interdisciplinary Computing
Empfohlen
Inheritance
Inheritance
Michal Píše
DEF CON 23 - Topher Timzen and Ryan Allen - hijacking arbitrary NET APP Contr...
DEF CON 23 - Topher Timzen and Ryan Allen - hijacking arbitrary NET APP Contr...
Felipe Prado
Object Detection with Tensorflow
Object Detection with Tensorflow
ElifTech
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
JSFestUA
constructor & destructor in cpp
constructor & destructor in cpp
gourav kottawar
06b extra methods
06b extra methods
Program in Interdisciplinary Computing
11 interfaces
11 interfaces
Program in Interdisciplinary Computing
CH 9: Editorial Style Notes
CH 9: Editorial Style Notes
Program in Interdisciplinary Computing
08 boolean expressions
08 boolean expressions
Program in Interdisciplinary Computing
14b exceptions
14b exceptions
Program in Interdisciplinary Computing
CH11 Graphics Notes
CH11 Graphics Notes
Program in Interdisciplinary Computing
01 intro to using java
01 intro to using java
Program in Interdisciplinary Computing
Chapter.08
Chapter.08
Program in Interdisciplinary Computing
Chapter.08
Chapter.08
Program in Interdisciplinary Computing
13 life and scope
13 life and scope
Program in Interdisciplinary Computing
13 interfaces
13 interfaces
Program in Interdisciplinary Computing
02 prepcode
02 prepcode
Program in Interdisciplinary Computing
Internet programming slide - java.ppt
Internet programming slide - java.ppt
MikeAdva
Python 如何執行
Python 如何執行
kao kuo-tung
Design patterns
Design patterns
Anas Alpure
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
AboutYouGmbH
Twins: OOP and FP
Twins: OOP and FP
RichardWarburton
04 variables
04 variables
Program in Interdisciplinary Computing
03 objects
03 objects
Program in Interdisciplinary Computing
The software design principles
The software design principles
Aman Kesarwani
Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?
PVS-Studio
06a methods original
06a methods original
Program in Interdisciplinary Computing
A brief overview of java frameworks
A brief overview of java frameworks
MD Sayem Ahmed
Ontopia tutorial
Ontopia tutorial
Lars Marius Garshol
Sour Pickles
Sour Pickles
SensePost
Weitere ähnliche Inhalte
Andere mochten auch
08 boolean expressions
08 boolean expressions
Program in Interdisciplinary Computing
14b exceptions
14b exceptions
Program in Interdisciplinary Computing
CH11 Graphics Notes
CH11 Graphics Notes
Program in Interdisciplinary Computing
01 intro to using java
01 intro to using java
Program in Interdisciplinary Computing
Chapter.08
Chapter.08
Program in Interdisciplinary Computing
Chapter.08
Chapter.08
Program in Interdisciplinary Computing
Andere mochten auch
(6)
08 boolean expressions
08 boolean expressions
14b exceptions
14b exceptions
CH11 Graphics Notes
CH11 Graphics Notes
01 intro to using java
01 intro to using java
Chapter.08
Chapter.08
Chapter.08
Chapter.08
Ähnlich wie 12 constructors
13 life and scope
13 life and scope
Program in Interdisciplinary Computing
13 interfaces
13 interfaces
Program in Interdisciplinary Computing
02 prepcode
02 prepcode
Program in Interdisciplinary Computing
Internet programming slide - java.ppt
Internet programming slide - java.ppt
MikeAdva
Python 如何執行
Python 如何執行
kao kuo-tung
Design patterns
Design patterns
Anas Alpure
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
AboutYouGmbH
Twins: OOP and FP
Twins: OOP and FP
RichardWarburton
04 variables
04 variables
Program in Interdisciplinary Computing
03 objects
03 objects
Program in Interdisciplinary Computing
The software design principles
The software design principles
Aman Kesarwani
Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?
PVS-Studio
06a methods original
06a methods original
Program in Interdisciplinary Computing
A brief overview of java frameworks
A brief overview of java frameworks
MD Sayem Ahmed
Ontopia tutorial
Ontopia tutorial
Lars Marius Garshol
Sour Pickles
Sour Pickles
SensePost
iOS training (basic)
iOS training (basic)
Gurpreet Singh Sachdeva
Dependency Injection
Dependency Injection
ColdFusionConference
Latest C++ Interview Questions and Answers
Latest C++ Interview Questions and Answers
DaisyWatson5
Swift, swiftly
Swift, swiftly
Jack Nutting
Ähnlich wie 12 constructors
(20)
13 life and scope
13 life and scope
13 interfaces
13 interfaces
02 prepcode
02 prepcode
Internet programming slide - java.ppt
Internet programming slide - java.ppt
Python 如何執行
Python 如何執行
Design 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, ...
Twins: OOP and FP
Twins: OOP and FP
04 variables
04 variables
03 objects
03 objects
The 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?
06a methods original
06a methods original
A brief overview of java frameworks
A brief overview of java frameworks
Ontopia tutorial
Ontopia tutorial
Sour Pickles
Sour Pickles
iOS training (basic)
iOS training (basic)
Dependency Injection
Dependency Injection
Latest C++ Interview Questions and Answers
Latest C++ Interview Questions and Answers
Swift, swiftly
Swift, swiftly
Mehr von Program in Interdisciplinary Computing
Phpmysqlcoding
Phpmysqlcoding
Program in Interdisciplinary Computing
Database basics
Database basics
Program in Interdisciplinary Computing
CGS2835 HTML5
CGS2835 HTML5
Program in Interdisciplinary Computing
Mysocial databasequeries
Mysocial databasequeries
Program in Interdisciplinary Computing
Mysocial databasequeries
Mysocial databasequeries
Program in Interdisciplinary Computing
CGS2835 HTML5
CGS2835 HTML5
Program in Interdisciplinary Computing
01 intro tousingjava
01 intro tousingjava
Program in Interdisciplinary Computing
Web architecture v3
Web architecture v3
Program in Interdisciplinary Computing
Xhtml
Xhtml
Program in Interdisciplinary Computing
Webdev
Webdev
Program in Interdisciplinary Computing
Web architecture
Web architecture
Program in Interdisciplinary Computing
Sdlc
Sdlc
Program in Interdisciplinary Computing
Mysocial
Mysocial
Program in Interdisciplinary Computing
Javascript
Javascript
Program in Interdisciplinary Computing
Javascript
Javascript
Program in Interdisciplinary Computing
Html5
Html5
Program in Interdisciplinary Computing
Frameworks
Frameworks
Program in Interdisciplinary Computing
Drupal
Drupal
Program in Interdisciplinary Computing
Database
Database
Program in Interdisciplinary Computing
Javascript2
Javascript2
Program in Interdisciplinary Computing
Mehr von Program in Interdisciplinary Computing
(20)
Phpmysqlcoding
Phpmysqlcoding
Database basics
Database basics
CGS2835 HTML5
CGS2835 HTML5
Mysocial databasequeries
Mysocial databasequeries
Mysocial databasequeries
Mysocial databasequeries
CGS2835 HTML5
CGS2835 HTML5
01 intro tousingjava
01 intro tousingjava
Web architecture v3
Web architecture v3
Xhtml
Xhtml
Webdev
Webdev
Web architecture
Web architecture
Sdlc
Sdlc
Mysocial
Mysocial
Javascript
Javascript
Javascript
Javascript
Html5
Html5
Frameworks
Frameworks
Drupal
Drupal
Database
Database
Javascript2
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.
11.
Overloaded Constructor LIS4930
© PIC
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
Jetzt herunterladen