File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes compilation fails why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "compilation fails why?" Watch "compilation fails why?" New topic
Author

compilation fails why?

Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 198
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

Joined: Sep 26, 2009
Posts: 188
what is the compilation error? Problem here looks like variable scoping.
sujith Acharya
Ranch Hand

Joined: Dec 25, 2006
Posts: 60
int l=this.k;


this.k can not be recognized as you are not declared this instance variable
Vishwanathan Nagarajan
Ranch Hand

Joined: Jan 28, 2009
Posts: 37
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.


MCP, SCJP 1.4, SCWCD 1.4, SCBCD 5.0, SCDJWS 1.4
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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!

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 198
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

Joined: Sep 19, 2008
Posts: 385
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.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Are you trying to do this ?



Time is what we want the most, but what we use the worst. -- William Penn
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compilation fails why?