• Post Reply Bookmark Topic Watch Topic
  • New Topic

instance variables and methods  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This code prints 200, when I dont have "System.out.println(ti.returnC();" in the main().
Now I added System.out.println(ti.returnC();" in the main() and when I execute this code, I get error message "non-static variable a and b cannot be referenced from a static context"
My question is, returnC() is accessed using instance ti. How is non static instance variable a and b accessed, which is inside the method ? If a nd b can be accessed in returnC(), then why is it giving error in returnD() ?

Thanks
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nirjari, please look at this line carefully.

You are trying to access non static variables a and b from main method.

main() method is static, since static content is known first to the runtime environment than the instance variables or methods you have to definitely create an instance and then use them. Since there is no instance to access a and b the error is raised.

Make it


I guess my answer is right. If anyone thinks its wrong please forgive me for making this attempt.

Have a good day.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code won't compile at all. You have weird semi-colons on lines 18 and 19...

If i correct those, the code does not compile as you state.

However, when i take out the "System.out.println(ti.returnC()", it STILL doesn't compile.

Check to make sure you are really compiling what you think you are, because something is not right here.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This code works fine. As Karthik explained, a and b are non static variable and need to be accessed using instance reference.

So variable c in returnC() ia also a non static variable. How come thats working in main() ?

Thanks
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot access non-static member variables from a static context (for example, in the main() method, which is static).

But when you call the method returnC() in the main() method, you are calling it on an instance of class TestInstance. You're calling ti.returnC(). This will return the value of the member variable c in the instance that ti is referring to.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we use " ti.returnD(a,b) " in the main(), we are using returnD() on the Instance ti of TestInstance class. So how come values of variablea a and b which is in ti instance are not used in returnD(a,b) ?

 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct that you are calling returnD on instance of ti. So nothing wrong with the method call, but the parameters being passed to the method i.e. a and b are non-static, and you are getting the error because of the variables not because of the method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!