Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8216468

single static reference variable can point to multiple instances of a class

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 8u172
    • Fix Version/s: None
    • Component/s: core-libs
    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_8

      Description

      A DESCRIPTION OF THE PROBLEM :
      created a static reference variable in class scope and outside the methods. Was able to access the static reference variable inside the main method and created multiple objects using the new keyword and assigned all the instances to the same static reference variable. In my opinion this should not be permitted because, multiple instances can never point to a single reference variable.

      Also, I was able to access the non static field inside the static main method through the static reference variable which again in opinion should never be permitted.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      expected a compile time error

      ---------- BEGIN SOURCE ----------
      package com.statictest;

      public class StaticCon {

      static StaticCon s1;


      StaticCon(){
      System.out.println("constructor invoked");
      System.out.println(this);
      }

      int x=1000;

      public static void main(String[] args) {

      s1 = new StaticCon();
      System.out.println(s1.x=1);
      s1 = new StaticCon();
      System.out.println(s1.x=2);
      s1 = new StaticCon();
      System.out.println(s1.x=3);

      }


      }
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              psonal Pallavi Sonal
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: