{"id":635,"date":"2016-01-19T14:09:29","date_gmt":"2016-01-19T14:09:29","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=635"},"modified":"2016-01-19T14:09:29","modified_gmt":"2016-01-19T14:09:29","slug":"instance-variable-example","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/instance-variable-example.html","title":{"rendered":"Instance Variable With Example In JAVA"},"content":{"rendered":"<p>An\u00a0instance variable\u00a0is a variable defined\u00a0in a class (i.e. a member\u00a0variable) in which each instantiated object of the class has a separate copy, or\u00a0instance. An instance variable\u00a0is similar to a class\u00a0variable.<\/p>\n<p>Instance variables belong to an instance of a class. It means instance variables belong to an object and we know that an object is an instance of a class. Every object has it\u2019s own copy of the instance variables.<\/p>\n<hr \/>\n<h4><strong>Instance Variable Declaration Example:<\/strong><\/h4>\n<pre>class Taxes\r\n{\r\nint count; \/\/Count is an Instance variable\r\n\/*...*\/\r\n}<\/pre>\n<p><strong>PROGRAM EXAMPLE WITH EXPLANATION:<\/strong><\/p>\n<p>Below is the program helps you to clearly understand the instance variables:<\/p>\n<pre>public class Record{\r\n\r\n\u00a0\u00a0 \u00a0public String name;\/\/ this instance variable is visible for any child class.\r\n\r\n\u00a0\u00a0 \u00a0private int age;\/\/ this instance age variable is visible in Record class only.\r\n\r\n\u00a0\u00a0 \u00a0public Record (String RecName)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0name = RecName;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public void setAge(int RecSal)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0age = RecSal;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public void printRec()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"name : \" + name ); \/\/ print the value for \u201cname\u201d\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"age :\" + age); \/\/prints the value for \u201cage\u201d\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public static void main(String args[])\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Record r = new Record(\"Ram\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0r.setAge(23);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0r.printRec();\r\n\u00a0\u00a0 \u00a0}\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>name : Ram\r\nage :23<\/pre>\n<p>In above example we take two instance variables one is <strong>name<\/strong> variable of string type and can be accessed by any child class because it is public and the second is <strong>age<\/strong> variable of integer type and can be accessed in the same class record because it is private.<\/p>\n<hr \/>\n<h4><strong>Important Points About Instance Variable:<\/strong><\/h4>\n<ul>\n<li>Instance variables are declared outside a method. It means they are declared in class.<\/li>\n<li>When an object is created with the use of the keyword &#8216;new&#8217; then instance variables are created and when the object is destroyed, instance variable is also destroyed.<\/li>\n<li>In Java, Instance variables can be declared in class level before or after use.<\/li>\n<li>For instance variables, access modifiers can be given.<\/li>\n<li>The instance variables are visible for all methods(functions), constructors and block in the class.<\/li>\n<li>Default values are given to instance variables. The default value is 0 for numbers, it is false for Boolean and it is null for object references.<\/li>\n<li>Instance variables can be accessed directly by calling the variable name inside the class.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>An\u00a0instance variable\u00a0is a variable defined\u00a0in a class (i.e. a member\u00a0variable) in which each instantiated object of the class has a separate copy, or\u00a0instance. An instance variable\u00a0is similar to a class\u00a0variable. Instance variables belong to an instance of a class. It means instance variables belong to an object and we know that an object is an &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/instance-variable-example.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Instance Variable With Example In JAVA<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,65],"tags":[],"class_list":["post-635","post","type-post","status-publish","format-standard","hentry","category-archieve","category-variables"],"psp_head":"<title>Instance Variable With Example In JAVA \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Understand concept of instance variable with example in JAVA. An instance variable is a variable defined in a class (i.e. a member variable) in which each instantiated object of the class has a separate copy, or instance.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/instance-variable-example.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=635"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/635\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}