Saturday, July 19, 2008

Can a local variable be static..? uhh..why not?

No! - that's a very straight forward answer to that question and most simple one. In java the local variables can not be static - burn this in your mind. But Why??. The local variables are those which are declared inside a method body, so they have scope only for the method execution period. Any static thing in java belongs to the class and not to the instance. Whenever the method is called the method variables are in the memory area of the thread executing that method, so they are on stack. On the other hand the objects and static members of a class are on heap. Static things are to live throughout the method calls and when they are inside the method body (on stack) they can't live outside the scope of method, hence its ridiculous and illogical to have static variables inside a method body.
For the exam, you need not know why, just remember that a local variable can never be static - compiler will complain.

Tuesday, July 8, 2008

File name and class name should be same?

There is a relation between the name of java source file and the public classes you can declare in that file - only the public classes..okay..not classes with any other access level. Hey by the way do you know which access modifiers can be applied to the outer classes/non-inner classes?...that will be a fantastic post in itself...coming soon.. :) So, coming back to the point, yes there is a constraint on the file name when you want to declare a class in the file as public. The constraint is that the file name should be same as that of your public class name.

But it's not that simple to understand by this single sentence, when I heard first time about this thing I had following doubts:

1. Can I have more than one public classes in my java file?

2. Should the class be public to be called by the JVM ?

3.If a class is not public but has a main method can it be called by the JVM? If yes then how the hell is it possible??? And if not ...why?? what's the problem in that??

There is no simple solution to these questions than coding for yourself and thats what I did!

Take a look at the code carefully, it's in the java source file named as

Let me tell you some things in this example. The source file name is and there is no class called as example6 in the source file. There are two classes class1 and class2 which are not public!

Doubts Answered:

1. NO! I tried to give public access to class1 and class2 and compiler scolded me for two things - one he said that class1 if public should be declared in file named, other is - there can't be any two public classes in a single java file.

2. Not al all! You can see that class1 and class2 are not public, still I am calling both of them!

3. Yes it can be called if it has a main method! You can see that class1 is not public but has a main method, so I can call it by giving parameter class1 to the java command.

I hope you have throughly understood the constraints. Just to revise all, here is the conclusion...

Conclusion: The above program proves that...

1.There is no need that the classes whose main method you are going to call should be public.

2.File name can be anything as long as you dont have any public classes in your file.

3.Which main method will execute only depends upon which classname you pass to the JVM that is as argument to java command.

4. If you want global access of a class and hence declare that class as public then it should be in the same file as the class name, and as you cant have two different names for a single file you can't have two public classes in a single .java file!

Sunday, July 6, 2008

Difference: positive zero 0 and negative zero -0 ?

Recently I have come across some strange things. I know that java language takes positive and negative zero differently than our logical mathematical brain will take. Well the two are certainly different but how does java identifies them? Or more basic question... does java even differenciate between positive and negative zero? The truth is that IEEE floating point specification differentiates between positive and negative zero, but the java specification says that positive zero is equal to negative zero. Please go through the following code and see if you can satisfy your questions...

I am still not able to understand why I am not getting -0 at 2nd and 4th place. I have posted this in javaranch forums, lets see if I can get some clarification. If you have the answer please comment.

Wednesday, July 2, 2008

Java is pass by value.....Always! - Part2

In the last post we saw what exactly pass by value and pass by reference means and strictly speaking its a fact that there is nothing like pass by reference in java. So we can say that java is pass by value...always! This post is a continuation so if you havn't please go through the first part - Java is pass by Value.....Always! - Part1

However note that, for the clarity of understanding the pass by value is when we pass a value and pass by reference is when we pass a reference to something.
Now lets look at the code which demonstrates pass by value and pass by reference.

Suppose there is a class TV, which contains a string which shows its state and its setters and getters.(Note:For the time please forget about the access modifiers..etc because this is just to make you understand one concept..I havnt run this program so it may have some errors/exceptions. But this code is perfect to make you understand what you need to at this point of time.)

class TV
string state; // this stores whether TV is currently ON or OFF

TV(string h)
this.state=h; //constructor assign a state for new TV object

setState(string s)

string getState()

The following code will demonstrate the pass by value with variable i and pass by reference with references remote1 and remote2.

class example1
int i=2;
public static void main()
TV remote1= new TV("OFF");

func2(int j)
System.out.println(j); // this will print 2
System.out.println(j); // this will print 3
System.out.println(i); // this will print 2

func3(TV remote2)
System.out.println(remote2.getState()); //this will print OFF
System.out.println(remote1.getState()); //this will print ON
System.out.println(remote2.getState()); //this will print ON


Here i and j are variables and they are independant of eachother. remote1 and remote2are references and they point to the same object, so both can turn on and off the same TV!

Have some doubts still? Go..on.. post a comment.. I will try my best to answer.