InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Java tutorials & examples

    Java Basics

    • Hello World
    • print and println
    • System.out.format
    • static and instance variables

    Java classes and objects

    • this keyword

    Java Strings

    • Initialize string array
    • String prefix match
    • compare string
    • string to int

    Java Arrays

    • Array basics
    • 2d array example
    • array for-each loop
    • fill an array

    Java cookbooks

    • method equals vs operator ==
    • print exception as string
    • Arrays.sort custom Comparator
    • create inline Comparator
     
    • Home
    • > Tutorials
    • > Java

    Java – static and instance variables

    on Jul 10, 2016

    Static variables are created once for a class and shared by all instances. Static variable is created the moment class is loaded.

    static variable example

    public class Main {
      public static int val = 0;
      public Main() {}
      public static void main(String[] args) {
        Main obj1 = new Main();
        obj1.val += 1;
        Main obj2 = new Main();
        obj2.val += 2; // val becomes 3 as both objects refer to same variable
        System.out.println(obj1.val);
        System.out.println(obj2.val);
      }
    }
    3
    3
    

    instance variable example

    public class Main {
      public int val;
      public Main() {
        val = 0;
      }
      public static void main(String[] args) {
        Main obj1 = new Main();
        obj1.val += 1;
        Main obj2 = new Main();
        obj2.val += 2; // both objects refer to different variables
        System.out.println(obj1.val);
        System.out.println(obj2.val);
      }
    }
    1
    2
    

    Suggested posts:

    1. bash – extract urls from xml sitemap
    2. Jenkins – how to setup build cron
    3. Java – equals vs ==
    4. View http headers in Chrome
    5. Docker quick start guide on Ubuntu
    6. CSS padding inside a box
    7. Java System.out.print and println
    8. CSS margin area of a box
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Java
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress