Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Method (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Accessor, mutator and manager methods== [[Accessor method]]s are used to read the data values of an object. Mutator methods are used to modify the data of an object. Manager methods are used to initialize and destroy objects of a class, e.g. constructors and destructors. These methods provide an [[abstraction layer]] that facilitates [[Encapsulation (object-oriented programming)|encapsulation]] and [[Modularity (programming)|modularity]]. For example, if a bank-account class provides a <code>getBalance()</code> accessor method to retrieve the current [[Balance (accounting)|balance]] (rather than directly accessing the balance data fields), then later [[revision control|revisions]] of the same code can implement a more complex mechanism for balance retrieval (e.g., a [[database]] fetch), without the dependent code needing to be changed. The concepts of encapsulation and modularity are not unique to object-oriented programming. Indeed, in many ways the object-oriented approach is simply the logical extension of previous paradigms such as [[abstract data types]] and [[structured programming]].<ref>{{cite book|last=Meyer|first=Bertrand|title=Object-Oriented Software Construction|year=1988|publisher=Prentice Hall International Series in Computer Science|location=Cambridge|isbn=0-13-629049-3|pages=52β54}}</ref> ===Constructors=== {{Main|Constructor (computer science)}} A [[Constructor (computer science)|''constructor'']] is a method that is called at the beginning of an object's lifetime to create and initialize the object, a process called [[object creation|construction]] (or ''instantiation''). Initialization may include an acquisition of resources. Constructors may have parameters but usually do not return values in most languages. See the following example in Java: <syntaxhighlight lang="java"> public class Main { String _name; int _roll; Main(String name, int roll) { // constructor method this._name = name; this._roll = roll; } } </syntaxhighlight> ===Destructor=== {{Main|Destructor (computer science)}} A ''[[Destructor (computer science)|Destructor]]'' is a method that is called automatically at the end of an object's lifetime, a process called [[object lifetime|Destruction]]. Destruction in most languages does not allow destructor method arguments nor return values. Destructors can be implemented so as to perform cleanup chores and other tasks at object destruction. ====Finalizers==== In [[Garbage collection (computer science)|garbage-collected]] languages, such as [[Java (programming language)|Java]],<ref name=Bloch>{{cite book | title= "Effective Java: Programming Language Guide" |last=Bloch| first=Joshua| publisher=Addison-Wesley | edition=third | isbn=978-0134685991| year=2018}}</ref>{{rp|26, 29}} [[C Sharp (programming language)|C#]],<ref name=Albahari>{{cite book |last=Albahari |first=Joseph |title= C# 10 in a Nutshell |publisher= O'Reilly |isbn= 978-1-098-12195-2}}</ref>{{rp|208-209}} and [[Python (programming language)|Python]], destructors are known as ''[[finalizer]]s''. They have a similar purpose and function to destructors, but because of the differences between languages that utilize garbage-collection and languages with manual memory management, the sequence in which they are called is different.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)