Saturday, July 19, 2008
Can a local variable be static..? uhh..why not?
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.
Let me tell you some things in this example. The source file name is example6.java 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 class1.java, 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 ?
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
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)
{
this.state=s;
}
string getState()
{
return(state);
}
}
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(i);
func3(remote1);
}
func2(int j)
{
System.out.println(j); // this will print 2
j=3;
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
remote2.setState("ON");
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.