title: Does making reference variable volatile, make all its field volatile too in java?
body: <p>I have read that making reference variable volatile, does not make  its inner fields volatile.But i tried with below example where it looks like volatile nature is applied to inner fields of class as well.</p>
  
  <p>User.java:-
  // user class having field "flag" set as true.</p>
  
  <pre><code>public class User {
  
      private boolean flag=true;
  
      public boolean isFlag() {
          return flag;
      }
  
      public void setFlag(boolean flag) {
          this.flag = flag;
      }
  } 
  </code></pre>
  
  <p>MyRunnableThread1.java:-</p>
  
  <p>here i have made "user" as volatile and not its inner field "flag" as volatile</p>
  
  <p>child thread is continuously in loop at "while(this.user.isFlag())".</p>
  
  <pre><code>public class MyRunnableThread1 implements Runnable {
  
      private String threadName;
      private  volatile User  user; 
  
      public MyRunnableThread1(String threadName,User user)
      {
          this.threadName=threadName; 
          this.user=user;
      } 
  
      public User getUser() {
          return user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }
  
      @Override
      public void run() {
  
          System.out.println("child thread:"+threadName+" started");
  
          while(this.user.isFlag()) {
  
          }
          System.out.println("child thread:"+threadName+" finished");
      }
  }
  </code></pre>
  
  <p>ThreadDemo.java:-</p>
  
  <p>in main thread we sets the value of field "flag" of "User" object as false
  to terminate the loop in child thread</p>
  
  <pre><code>public class ThreadDemo {
  
      public static void main(final String[] arguments) throws InterruptedException {
  
          System.out.println("main thread started");
  
          User user=new User(); 
          MyRunnableThread1 myRunnableThread1=new MyRunnableThread1("Thread1",user);
          Thread t=new Thread(myRunnableThread1);
          t.start();
  
          try {
              Thread.sleep(6000);
          }
          catch(Exception e) {
              System.out.println("exception in sleep:"+e);
          }
  
          myRunnableThread1.getUser().setFlag(false);
  
          System.out.println("main thread finished"); 
      }
  }
  </code></pre>
  
  <p>o/p:-</p>
  
  <p>main thread started
  child thread:Thread1 started
  main thread finished
  child thread:Thread1 finished</p>
  
  <p>In above example I have made "user" variable as volatile in "MyRunnableThread1.java" class.
  User object has field "flag" which is true at time of instantiation.
  After starting the child thread where it continuously execute the loop , the main thread changes the value of field "flag" of user object to false.
  But here field "flag" is not volatile instead the "user" reference variable is volatile.But still here  the change in value of field "flag" in main thread is reflected in child thread.It behaves as if field "flag" is also volatile.
  can anyone help with above issue??</p>