{"id":3806,"date":"2017-07-31T06:03:55","date_gmt":"2017-07-31T06:03:55","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=3806"},"modified":"2021-03-21T06:19:33","modified_gmt":"2021-03-21T06:19:33","slug":"static-variables-vs-instance-variables","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/static-variables-vs-instance-variables\/","title":{"rendered":"Static variables vs Instance Variables"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>In this tutorial, we will learn the difference between static variables and instance variables in Java language.<\/p>\n<h3>Static variables<\/h3>\n<p>Static variables belong to the class. There are also known as class variables. <strong>countDogObjects <\/strong>is a static int variable in the example. static variables have a single copy of the variable for the class and are independent of the objects of the class. In the example, we use the static variable to count the number of <strong>Dog<\/strong> objects created in the program.<\/p>\n<p>We can access the static variable using the class name.<\/p>\n<p><strong>className.staticVariable;<\/strong><\/p>\n<p><strong>className.getter_method_for_static_variable();<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3826\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-class-and-Objects.png\" alt=\"Dog class and Objects\" width=\"1719\" height=\"881\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-class-and-Objects.png 1719w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-class-and-Objects-300x154.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-class-and-Objects-1024x525.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-class-and-Objects-768x394.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-class-and-Objects-1536x787.png 1536w\" sizes=\"auto, (max-width: 1719px) 100vw, 1719px\" \/><\/p>\n<h3><\/h3>\n<h3>Instance variables<\/h3>\n<p>Instance variables have their own copy for each object. These variables are also known as object variables. These variables are used to persist the object state. In the example, the variables <strong>name, weight, height<\/strong> of the <strong>Dog<\/strong> class are instance variables.<\/p>\n<p>We can access the instance variable using the class name.<\/p>\n<p><strong>objectReference.staticVariable; \/\/inside the class if private<br \/>\n<\/strong><\/p>\n<p><strong>objectReference.getter_method_for_the_variable();\/\/outside the class<br \/>\n<\/strong><\/p>\n<h3>Code Listing<\/h3>\n<h3>Dog.java<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.testingdocs.tutorial.dog;\r\n\r\n\/\/Dog.java\r\n\/\/www.TestingDocs.com\r\n\r\npublic class Dog {\r\n  private String name;\r\n  private double weight;\r\n  private double height;\r\n  public static int countDogObjects=0;\r\n\r\n  \/\/ constructor\r\n  public Dog(String name, double weight, double height) {\r\n    this.name = name;\r\n    this.weight = weight;\r\n    this.height = height;\r\n    countDogObjects++;\r\n  }\r\n\r\n  public String getName() {\r\n    return name;\r\n  }\r\n\r\n  public void setName(String name) {\r\n    this.name = name;\r\n  }\r\n  public double getWeight() {\r\n    return weight;\r\n  }\r\n\r\n  public void setWeight(double weight) {\r\n    this.weight = weight;\r\n  }\r\n\r\n  public double getHeight() {\r\n    return height;\r\n  }\r\n\r\n  public void setHeight(double height) {\r\n    this.height = height;\r\n  }\r\n\r\n  public static int getCountDogObjects() {\r\n    return countDogObjects;\r\n  }\r\n\r\n  @Override\r\n  public String toString() {\r\n    return \"name=\" + name + \", weight=\" + weight + \", height=\" + height ;\r\n  }\r\n\r\n}\r\n<\/pre>\n<h3>DogDriver.java<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.testingdocs.tutorial.dog;\r\n\r\n\/*********************************************\r\n * DogDriver.java\r\n * www.TestingDocs.com\r\n * \r\n *********************************************\/\r\npublic class DogDriver {\r\n\r\n  public static void main(String[] args) {\r\n    Dog dogOneObject= new Dog(\"Tommy\",1.5,0.25);\r\n    Dog dogTwoObject= new Dog(\"Johny\",2.5,0.75);\r\n    System.out.println(\"Dog One=\" + dogOneObject.toString());\r\n    System.out.println(\"Dog Two=\" + dogTwoObject.toString());\r\n    System.out.println(\"Number of Dog objects created =\"+ Dog.getCountDogObjects());\r\n  }\r\n\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3829\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-Class-Java.png\" alt=\"Static variables\" width=\"1735\" height=\"994\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-Class-Java.png 1735w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-Class-Java-300x172.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-Class-Java-1024x587.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-Class-Java-768x440.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Dog-Class-Java-1536x880.png 1536w\" sizes=\"auto, (max-width: 1735px) 100vw, 1735px\" \/><\/p>\n<h3>Sample Output<\/h3>\n<p>Dog One=name=Tommy, weight=1.5, height=0.25<br \/>\nDog Two=name=Johny, weight=2.5, height=0.75<br \/>\nNumber of Dog objects created =2<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-3806\" data-postid=\"3806\" class=\"themify_builder_content themify_builder_content-3806 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this tutorial, we will learn the difference between static variables and instance variables in Java language. Static variables Static variables belong to the class. There are also known as class variables. countDogObjects is a static int variable in the example. static variables have a single copy of the variable for the class and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-3806","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=3806"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3806\/revisions"}],"predecessor-version":[{"id":19721,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3806\/revisions\/19721"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=3806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=3806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=3806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}