File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compilation fails why?

 
Ankit Tripathi
Ranch Hand
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • 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();

}
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the compilation error? Problem here looks like variable scoping.
 
sujith Acharya
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int l=this.k;


this.k can not be recognized as you are not declared this instance variable
 
Vishwanathan Nagarajan
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to do this ?

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic