• 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

compilation fails why?

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is wrong in the given code???

public class StaticTest
{
static
{
int i=5;j=6;
int k=i*j;
}
public StaticTest()
{
System.out.print("in the constructer");
int l=this.k;
System.out.println(l);
}

public static void main(String s[])
{

StaticTest s1=new StaticTest();

}
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the compilation error? Problem here looks like variable scoping.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

int l=this.k;



this.k can not be recognized as you are not declared this instance variable
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following are the issues with your code;

1.

public static void main(String s[])
{

StaticTest s1=new StaticTest();

}



you need to include a } at the end
2. inside the static block, j should also have a type specifier as; int j = 6 (or) int i = ...,j=...;
3. you don't need to use the this keyword inside the constructor
4. you cannot access the variable k that is inside the static block inside the constructor. Basically any static member should be accessed from any other static block or method or directly through the class.

I would suggest you brush up with the basics of java before continuing with your coding.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a static initializer in your class:

The static initializer declares some local variables i, j and k. Later in your code your are accessing 'this.k', as if k is a member variable of the class. It is not.

Declaring variables inside a static initializer block does not make them member variables.
Your seem to think that the above code is the same as this:

But it is not the same!
 
Ankit Tripathi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could have been check this twice or thrice before sending it.......... I have marked a brace at the end of snip.
The code has been compiled when int k is static but when i am putting k in a static block it hasn' been compiled...please specify the reason...... I am a newcomer to java.
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason it fails because the variable K is not available in StaticTest method.

If you declare any variable inside block or method then that variable is avaliable only inside that block or method unless you pass the reference out of the mrthod.

But if you declare your variable in a class and outside any of the code block or method then all the methods and code blocks inside that class can access it either by using reference or directly which depends on variable type (class or instance).

So in your case your variable k has lifetime only during the code block runs and then it dies and no more avaialbe.

But if you would have declared it inside the class and outside any of the method or code block you could use it anywhere in the class.
 
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you trying to do this ?

 
There’s no place like 127.0.0.1. But I'll always remember 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