Results 1 to 20 of 26
Thread: main method String
- 09-19-2011, 06:52 PM #1
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 44
- Rep Power
- 0
- 09-19-2011, 07:00 PM #2
Re: main method String
you are missing the argument in your main
make that change, recompile and try it again.Java Code:public class Sample { public static void main(String[] args) { System.out.println("Hai"); } }Last edited by sehudson; 09-19-2011 at 07:20 PM.
- 09-19-2011, 07:13 PM #3
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 44
- Rep Power
- 0
Re: main method String
Sir, Idont want create a pakage.... Iam asking why the java interpreter sends an error msg while running the above program....
- 09-19-2011, 07:20 PM #4
Re: main method String
ignore the package name. just add the String[] args inside the main parenthesis.
- 09-19-2011, 07:24 PM #5
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 44
- Rep Power
- 0
Re: main method String
Sir, that's iam saying,
If we write string args[] in main it gives the o/p. but if we dont write the program complied successfully.. But while running that program it shows an error msg. why?
- 09-19-2011, 07:24 PM #6
Member
- Join Date
- Sep 2011
- Location
- Mumbai, India
- Posts
- 35
- Rep Power
- 0
Re: main method String
Java interpreter sends the message because it is looking for the method with the signature public static void main(String[] a)
and you have not written that. Secondly, you are telling that you are getting the error java.lang.NoSuchClass error which i think you should not get and you
will get java.lang.NoSuchMethodError
- 09-19-2011, 07:30 PM #7
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 44
- Rep Power
- 0
Re: main method String
Thank you Rameshwar.
It means it's essential thing to write string args[] in main... without writing this we cannot run this right?
But in c/cpp we dont declare any variable..... why it's essential to write in java?
- 09-19-2011, 07:41 PM #8
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 44
- Rep Power
- 0
Re: main method String
if i dont want to parse the values to main, so in this case it's not essential to write string args[] in main right?
- 09-19-2011, 07:46 PM #9
Re: main method String
The java program only looks for a main(String[] ) method as the starting point to the program.
Ask the author of the java program why he only wanted that one entry point.
Its not important enough to argue about. It works. use it.
- 09-19-2011, 07:51 PM #10
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 44
- Rep Power
- 0
Re: main method String
try this:
class sample
{
static
{
sample.main();
System.exit(0);
}
public static void main()
{
System.out.println("Hai");
}
}
In the above program the main method dint recive any values. and you run this above progrm you get the o/p. why?
- 09-19-2011, 07:53 PM #11
Member
- Join Date
- Sep 2011
- Location
- Mumbai, India
- Posts
- 35
- Rep Power
- 0
Re: main method String
This is what the Java creators had decided. It should be there means it should be there and we have to follow certain rules.
Why 2+2=4 and not 5. I don't know but it is said 4 and it is 4. Anyways See it is essential in Java because what would you do if you want to pass certain arguments at run-time? So what ever you pass at run-time is stored in String array
which you declare in main method. I think an example is best to explainyou will run this program java Demo 2 3 and the output is 5.Java Code:class Demo { public static void main(String[] args) { int ans=(Integer.parseInt(args[0])) +(Integer.parseInt(args[2])); System.out.println(ans) ; } }
So you can see how that array in main method was used.
- 09-19-2011, 07:55 PM #12
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 44
- Rep Power
- 0
Re: main method String
ya i know this one. if i dont send the command line arguments, then?
- 09-19-2011, 07:58 PM #13
Re: main method String
In your code sample you could have called the method anything. It didn't have to be main.
Java Code:class Sample { static { Sample.startHere(); System.exit(0); } public static void startHere() { System.out.println("Hai"); } }
- 09-19-2011, 08:01 PM #14
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 44
- Rep Power
- 0
Re: main method String
ya i know. bcz the static is block which was called before the main method called, right.
- 09-19-2011, 08:03 PM #15
Re: main method String
The main method in your code would not be called by the java program. It's Your code that calls it and it could be with any name as far as java is concerned.
- 09-19-2011, 08:05 PM #16
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 44
- Rep Power
- 0
Re: main method String
is main method is predefined or userdefined method?
- 09-19-2011, 08:07 PM #17
Re: main method String
Please explain what is predefined and what is userdefined.
- 09-19-2011, 08:10 PM #18
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 44
- Rep Power
- 0
Re: main method String
both(pre and user defined datatype).
Bcz the main method was created by user. that's way we called it as user defined. and
every java program excution was starts with main method. it's predefined. so that's way we called predfined. Right?
- 09-19-2011, 08:13 PM #19
Re: main method String
That is correct. I type in/create all of the code in my programs.the main method was created by user. that's way we called it as user defined.
That is correct. The java program calls a public static main(String[]) method.every java program excution ... starts execution with main method. it's predefined.
- 09-19-2011, 08:16 PM #20
Member
- Join Date
- Sep 2011
- Location
- India
- Posts
- 44
- Rep Power
- 0
Similar Threads
-
Main Method
By maya700 in forum New To JavaReplies: 13Last Post: 07-23-2011, 08:41 AM -
main method
By 007 in forum New To JavaReplies: 18Last Post: 06-25-2011, 01:47 PM -
Running main method class from another main class
By tlrocketman in forum New To JavaReplies: 3Last Post: 12-06-2010, 08:30 AM -
Calling The main method from another method
By SwissR in forum New To JavaReplies: 3Last Post: 07-27-2010, 11:03 AM -
calling method from main method
By bob_bee in forum New To JavaReplies: 4Last Post: 10-02-2009, 05:30 PM


1Likes
LinkBack URL
About LinkBacks


Bookmarks