• Post Reply Bookmark Topic Watch Topic
  • New Topic

access modifiers for variables  RSS feed

 
sireesha vadlamani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does a variable have public access modifier? if we can use it within the class and outside of the class then can i access a public variable as follows??

ERROR: it shows 6 errors :-O. Can anybody explain the errors 1.illegal start of the expression
2. class,interface, or enum expected
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Access modifiers are only allow outside of methods. A method can be public. A class can be public. An instance variable can be public.

Inside a method, a variable is always a local variable. It isn't accessible outside the method, so access modifiers don't make sense.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always remember that there is only one type modifier that can be declared on a variable that has local scope and that's the non-access modifier final
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sireesha vadlamani wrote:
ERROR: it shows 6 errors :-O. Can anybody explain the errors 1.illegal start of the expression
2. class,interface, or enum expected


Does the above code get those errors? Mine only complains about the "public" on a local variable.

BTW, the convention in Java is to have class names start with an uppercase letter.
 
sireesha vadlamani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
sireesha vadlamani wrote:
ERROR: it shows 6 errors :-O. Can anybody explain the errors 1.illegal start of the expression
2. class,interface, or enum expected


Does the above code get those errors? Mine only complains about the "public" on a local variable.

BTW, the convention in Java is to have class names start with an uppercase letter.

yes, i got those errors. And without the uppercase also the program will be executed and it give the output also. As i know, we will give the starting letter of a class name as uppercase because it easy to identify for the compiler that, that is class name and for user understandable purpose also.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sireesha vadlamani wrote:As i know, we will give the starting letter of a class name as uppercase because it easy to identify for the compiler that, that is class name and for user understandable purpose also.

The compiler does not care what the name is (as long as it is legal). For the compiler names like BufferedReader or clаss or ___$3289ud9jłłłł漢字ひらがな are equally fine.
 
ajay smith
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a pet peeve of mine: on lines 10 and 11, you create an instance of mo, and then use that instance to call the display() method. Java doesn't let you call another method by instance from a static method. Therefore, your main method, which is static, cannot call display() by m.display(). Instead, replace lines 10 and 11 with just display();
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ajay smith wrote:Just a pet peeve of mine: on lines 10 and 11, you create an instance of mo, and then use that instance to call the display() method. Java doesn't let you call another method by instance from a static method. Therefore, your main method, which is static, cannot call display() by m.display(). Instead, replace lines 10 and 11 with just display();

This is not correct.
display() is an instance method and therefore you can't call it directly from a static method (ie main()). You need a reference to an instance (ie mo) to be able to call an instance method (ie display()) so the code is correct (apart from the issues with the visibility modifier pointed out in earlier posts).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!