• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Instance/Static initializers

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below does not compile because of the PUBLIC modifier. If you remove PUBLIC the compile is happy however is you include any modifier the compiler barks at you.
Can anyone explain why this is not possible?
public class Test {
{
public int g = 0;
if(g > 0)
System.out.println("Can compile");
}
}


------------------
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, this is a bit of a guess but I think it's a good guess - I'd be grateful if anyone could confirm it. Because you're declaring g inside the initialiser, it's effectively a method variable and you can't put access modifiers in front of method variables.
So what you're probably wanting to do is this:-
public class Test {
public int g;
{
g = 0;
if(g > 0)
System.out.println("Can compile");
}
}
So g is now a class member variable.
Hope this helps,
Kathy
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java compiler has problem with you are declared int i as public
there is no problem with public class Test//
Remember you never declare local varible(in method,in static block or simple block) with any access modifier.
bye.......................bye
 
oluwayomi adegoju
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much. I now understand why.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! oluwayomi,
i think it just scope problem. in instance initialize block your variable g just work within his {}. there4 u cant declare public g variable....hope this help.
if m i wrong plz tell me.
thanx
Aftab
 
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic