• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variable Example

 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Any idea why below code does not display 'instance variable first' as it is the first value assigned to my Var here in this example?

[code=java]

public class VariableExample {
  // instance variable
  public String myVar="instance variable";
   
  public void myMethod(){
    // local variable
    String myVar = "Inside Method";
    System.out.println(myVar);
  }
  public static void main(String args[]){
     // Creating object
     VariableExample obj = new VariableExample();
   
     /* We are calling the method, that changes the
      * value of myVar. We are displaying myVar again after
      * the method call, to demonstrate that the local
      * variable scope is limited to the method itself.
      */
     System.out.println("Calling Method");
     obj.myMethod();
     System.out.println(obj.myVar);
  }
}

[/java]
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
..Just arranged the same code in proper format here ..




Thanks
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the current output from the program along with comments about what you are talking about?
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
below is the output:-


F:\Java Training\Java Programs>java  VariableExample
Calling Method
Inside Method
instance variable

I meant to ask that in this program example first System.out.println(myVar) according to my understanding should have been printed as 'instance variable' but  program prints it as 'Calling Method'
I am unable to understand this.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as an exercise, can you edit the source program and add numbers as comments to indicate the order of statement execution?  That numbering will show use what you think are the order of the print statements.
For example here are the first two statements to be executed:
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please find my understanding with respect to print statements sequence:-

 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code does NOT execute in the order of the statements in the source file.  When a java program is started, the first statement to execute is in the main() method.  From there execution will follow what the statements say to do.  Many statements will execute in their order in the source, but there are lots of statements that will change the order, like method calls, loops, if statements.

Can you redo the numbering to show what order the statements are executed in, not their order in the source code.  See my post for where I thought the first two statements to be executed are:  // 1 and // 2

Note Use // to make the comments, not /*
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Afraid that is incorrect. Follow the execution of the code with a pencil, and you will see where you were mistaken.
 
John Ken
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok,i got your point but is there any way i can debug line by line to get a feel how compiler is executing instructions written in this program so that it would give me better understanding as i may assume anything as per my limited skills that this should execute 1st then this hen this ..blah,blah..blah then finally this. but if iam able to debug it then it would give me more clarity.

so any idea how could i debug it in notepad or in eclispse?


Thanks
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Eclipse try Run > Debug, or just press F11.
 
Forget Steve. Look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic