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
Instance variable
(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!
{{Short description|Member variable of a class that all its objects possess a copy of}} {{distinguish|class variable}} {{multiple issues| {{original research|date=September 2013}} {{tone|date=September 2013}} }} In [[class-based]], [[object-oriented programming]], an '''instance variable''' is a [[variable (programming)|variable]] defined in a [[Class (computer programming)|class]] (i.e., a [[member variable]]), for which each instantiated [[Object (computer science)|object]] of the class has a separate copy, or [[Instance (computer science)|instance]].<ref>{{Cite web |title=Instance Variables in C++ Programming |url=https://www.dremendo.com/cpp-programming-tutorial/cpp-instance-variables |access-date=2024-03-08 |website=Dremendo |language=en}}</ref><ref>{{Cite web |date=2017-02-06 |title=Java Variables |url=https://www.geeksforgeeks.org/variables-in-java/ |access-date=2024-03-08 |website=GeeksforGeeks |language=en-US}}</ref> An instance variable has similarities with a [[class variable]],<ref>{{cite web|title=The Java Tutorial, Variables|url=http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html|website=docs.oracle.com|publisher=Oracle|access-date=23 October 2014|archive-url=https://web.archive.org/web/20141023153904/http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html|archive-date=23 October 2014}}</ref> but is non-[[Static variable|static]]. An instance variable is a variable which is declared in a class but outside of [[Constructor (object-oriented programming)|constructor]]s, [[Method (computer programming)|method]]s, or [[Block (programming)|blocks]]. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class. [[Access modifiers]] can be given to the instance variable. An instance variable is not a [[class variable]],<ref>{{Cite web |date=2021-04-26 |title=Difference between Instance Variable and Class Variable |url=https://www.geeksforgeeks.org/difference-between-instance-variable-and-class-variable/ |access-date=2024-03-08 |website=GeeksforGeeks |language=en-US}}</ref> although there are similarities. Both are a type of [[Class (computer science)#Structure|class attribute]] (or class property, [[Field (computer science)|field]], or data member). While an instance variable's value may differ between instances of a class, a [[class variable]] can only have one value at any one time, shared between all instances. The same dichotomy between ''instance'' and ''class'' members applies to methods ("member functions") as well. Each instance variable lives in [[Computer memory|memory]] for the [[Object lifetime|lifetime]] of the object it is owned by.<ref>{{cite web|title=The Java Tutorials, Understanding Class Members|url=http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html|website=docs.oracle.com|publisher=Oracle|access-date=23 October 2014|archive-url=https://web.archive.org/web/20141011000515/http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html|archive-date=11 October 2014}}</ref> Instance variables are properties of that object. All instances of a class have their own copies of instance variables, even if the value is the same from one object to another. One class instance can change values of its instance variables without affecting all other instances. A class may have both instance variables and [[Class variable|class variables]]. Instance variables can be used by all instance methods of an object, but may not be used by class methods. An instance variable may also be changed directly, provided [[Access modifiers|access restrictions]] are set.<ref>{{cite web|last1=Matuszek|first1=David|title=Static|url=http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/static.html|website=cis.upenn.edu|publisher=University of Pennsylvania|access-date=23 October 2014|archive-url=https://web.archive.org/web/20141023160745/http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/static.html|archive-date=23 October 2014}}</ref>
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)