Hi
HERE is mu program .. it works fine no problem with that .. but it only compiles when i remove public specifier from the class " Horsetest" .. why is this happening ?? any ideas .. in c# if we declare like this program will execute no compile error .. why
Program :
public class HorseTest
{
public static void main (String [] args)
{
class Horse
{
public String name;
public Horse(String s)
{
name = s;
}
}
Object obj = new Horse("Zippo");
Horse h = (Horse) obj;
System.out.println(h.name);
}
}
please help me :) cheers
Loading

VulpesPosted Oct 26, 2013, 7:28 PM
SUNIL GUTTAPosted Oct 27, 2013, 3:06 PM
VulpesPosted Oct 27, 2013, 3:04 PM
SUNIL GUTTAPosted Oct 27, 2013, 2:58 PM
One final question in this post ..
Well you said when we keep the file name same as class name (which is fallowed in Real time)
in java the clas can be made as public right ,,
What if i made a c# code file with class public and different file name .. but still it is executing perfectly ?? any reason or am i wrong . .
cheers & ty for ur continued help
VulpesPosted Oct 27, 2013, 2:52 PM
So, when you use that site to compile your Java source code, I'd avoid using public classes.
SUNIL GUTTAPosted Oct 27, 2013, 2:38 PM
VulpesPosted Oct 27, 2013, 9:10 AM
Certainly, my desktop Java compiler isn't bothered whether the main class is public or not as long as the file name and class name are the same.
What error message do you get?
SUNIL GUTTAPosted Oct 27, 2013, 5:46 AM
but small thing .. when i tried to compile online .. its showing compile error when keeping public to class ..
why is it ..
VulpesPosted Oct 27, 2013, 5:19 AM
Even if the 'main class' isn't public, it's still considered good practice to give the file the same name as the class.
Although I'm not keen on the practice personally, it's not uncommon to nest classes within other classes even in C#.
However, one thing you can do in Java but not (thankfully) in C# is to nest a class within a method as you did in your example.
Java has packages rather than namespaces which fulfil the role of avoiding name collisions and some other things besides.
SUNIL GUTTAPosted Oct 26, 2013, 7:46 PM
java program is bit confusing compared to c# ..
In c# we rarely declare a class in side a class right ?
what were these name spaces in c# called in java ?? does they exist ?
TY
SUNIL GUTTAPosted Oct 26, 2013, 7:39 PM
So public to the above code is nothing to do with main method ..
My friend said if a class contains a main method in it it is called main class .. then the main class should'nt be public must he said ?? ..
is he right ..
Thank you