• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

problem with variables

 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried compiling this code


and it works but if I change this to
class Test
{
int a;
a=1;
}
It gives an error saying
C:\j2sdk1.4.0\bin>javac test.java
test.java:4: <identifier> expected
a=1;
^
test.java:4: cannot resolve symbol
symbol : class a <-- class a
location: class Test
a=1;
^
2 errors
Why is this happening and why is the symbol showing a as a class.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you declare a variable you can assign it a value. It is the special circumstance of its declaration that allows you to do that outside a method. But you cannot start manipulating that value OUTSIDE of a method or constructor.


here the variable "a" has been declared and initialized to the value 0.
when you do this:

You are trying to assign another value to "a"; this can only be done in a method or a constructor.
Regarding the errors: It looks like the compiler thinks this may be an attempt at an inner class, or another variable.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Originally posted by Larry Jones:
You are trying to assign another value to "a"; this can only be done in a method or a constructor.


Not only we can assign in a method and construtor, but also in blocks.
public class Test {
int a;
{
a=100;
}
public static void main(String[] args){
Test t = new Test();
System.out.println(t.a);
}
};
But i dont know why this is showing error.
Can anybody explain me
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Badrinath,
I've tried your code snippet and I don't seem to get any errors.

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ravi,
the error will come in code posted by anupam

since it is not in any block,constructor or method.
Pinky
 
Badri Sarma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

i want to know the difference between
case 1
int a;
a=100;
and
case 2
int a;
{
a=100;
}
why in case 1 we get error and case 2 we wont
 
pinky yadav
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Badrinath,
in case 1
int a; // defines a member variable of type a
a=1; // gives a compile time error as it is not part of any block or constructor or method.
in case b
{ a= 1;} // compiler identifies it as a instance initializer block as it is enclosed in braces. this block is executed befor the constructor is called when the object is made.
hope this is helpful
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic