java program without main method
Hi every one i want to ask you a Question that :-----> Can we made a java program without main method be sure it should not be an applet?
Second Q. :--> I want to know all the method's of Array declaration in java??
Third Q. :----> I want to know does main method of java is always a primary thread for java can we change the sequence of main thread from another than please tell me???
Hey please send a detail's explanation of these Question if possible than send a small program with ans of these Question...
Re: java program without main method
i too want the answer for these questions buddy,any body help plz..
Re: java program without main method
Hi..
For the question 1:
Yes,We can write a Java program which does not have main method at all. This can be done using the static block of the class.
How this works is that static initialization blocks get executed as soon as the class is loaded, even before the main method is called. During run time, JVM will search for the main method after exiting from this block. If it does not find the main method, it throws an exception. To avoid the exception System.exit(0); statement is used which terminates the program at the end of the static block itself.
class WithoutMain
{
static
{
System.out.println("This java program ran without the main method");
System.exit(0);
}
}
For the question 2:
Some examples:
Exception ae[] = new Exception[3];
Object aao[][] = new Exception[2][3];
int[] factorial = { 1, 1, 2, 6, 24, 120, 720, 5040 };
char ac[] = { 'n', 'o', 't', ' ', 'a', ' ',
'S', 't', 'r', 'i', 'n', 'g' };
String[] aas = { "array", "of", "String", };
For question 3:
I think,we can change the sequence,but,i am not sure.
Thanks n' Regards
Neelima
Re: java program without main method
Thanks neelim It's the ideal solution for me....///
Re: java program without main method
sorry for the answer given by neelim for the question number 1.
No java program executes without main method.
until unless u wil call that class WithoutMain in the main method , static block will not produce output
Re: java program without main method
[QUOTE]sorry for the answer given by neelim for the question number 1.
No java program executes without main method.
until unless u wil call that class WithoutMain in the main method , static block will not produce output[/QUOTE]
Neelim's answer for Question Number 1 is correct...
it will generate o/p.
Memory for the Satic members will be allocated during the compile time itself. and the Interpreter looking for the main method during the execution time if it is not avail , then it throws an java.lang.NoSuchMethodError.if we use System.exit(0) then the programs perfectly run...
Re: java program without main method
Actually both Neelim & Umeshap are correct to an extent. The code provided by Neelim though correct will not execute by itself as it has no main. So what it needs is another class instantiating it (ie. loading it) as below:
public class NoMain
{
static
{
System.out.println("This is an executable class even without a [INDENT][/INDENT]main method");
System.exit(0);
}
}
public class NoMainTest
{
public static void main(String[] args)
{
new NoMain();
}
}
So yes we can have a java prg without the main method...
Hope this helps.
Re: java program without main method
can anybody tell wats the use of THIS keyword in java??????????
Re: java program without main method
[QUOTE=raghavmishra;55556]can anybody tell wats the use of THIS keyword in java??????????[/QUOTE]
Hi Raghavmishra,
The keyword [B]this[/B] is useful when you need to refer to instance of the class from its method. The keyword helps u to avoid name conflicts.
Eg:
public class GeekTalk
{
final static int MAXX=640, MAXY=480;
int x, y;
GeekTalk(int x, int y)
{
[B]this.x = x;
this.y = y;[/B]
}
GeekTalk()
{
[B]this(MAXX/2, MAXY/2);[/B]
}
public String toString()
{
return "[" + x + "," + y + "]";
}
public static void main(String[] av)
{
System.out.println(new GeekTalk(300,100));
System.out.println(new GeekTalk());
}
}
Thanks,
Riju's.
Re: java program without main method
this code is very tough to understand riju i m beginer so can u give some simple code....for this keyword
Re: java program without main method
Neelam's answer is correct,recently i heard about this concept
yaa we can execute an java program without main method..
Thanks
Sagar Chugh
Re: java program without main method
Ur first answer:
Yes,u can make a java program without main().You can also compile it and a .class file will be generated after compilation.But you cannot execute it,because when you try to execute this .class file, you will get a runtime error called java.lang.NoSuchMethodError:main.
Ur second answer:
The various ways in which we can declare an array are:
First way:
Here,only an array is created but elements in the array get default values.
int [] arr=new int [5];
Second way:
int arr[]=new int[10];
Third way:
Here in addition to declaring the array,you are also giving the values to the elements in the array.Here,you dont need to specofy the size of array.Depending on the number of entries in the curly braces i.e{},the size is automatically assigned to the array.So,here the size of array becomes 4.
int arr[]={1,2,6,9};
Fourth way:
int arr[]=new int [] {1,2,3};
Ur third answer:
main() will always be the first thread to be executed.When the java run time executes the program,the first thread to be executed is main().From inside main(),other threads are created and then those threads are started.Each thread has got a priority.In java, priority of main() is 5.You can set the priority of other threads greater than the priority of main().So,if you set the other thread at a greater priority,that thread will be given more importance by the JVM.But,main() must be the first thread executed by the JVM and other threads can be started only from within main. Also main() is the last thread to be executed before the program ends.
Re: java program without main method
[QUOTE=raghavmishra;55556]can anybody tell wats the use of THIS keyword in java??????????[/QUOTE]
It returns the reference to the current object
Re: java program without main method
[QUOTE=raghavmishra;55556]can anybody tell wats the use of THIS keyword in java??????????[/QUOTE]
The keyword this always refers to the current object i.e the object on whose reference the current action is being executed.
If both the local variable and the instance variable have the the same name inside a method,then the local variable always shadows the instance variable.So,how will you access the instance variable inside the method if the locale variable also has the same name as instance variable?In those cases we can use 'this' keyword.
We can understand it with an example:
1.class Example
2.{
3. private int a;
4. public void setA(int a)
5. {
6. a=a;
7. }
8.}
9.class UseExample
10.{
11. public static void main(String args[])
12. {
13. Example e=new Example();
14. e.setA(5);
15. }
16.}
I have included the line number in previous example to make it more clear.In this program,we have created an object on line 13.We want to set the value of this object's instance variable 'a' to value say,5.We use the setA() method defined on line 4.But in setA(),local variable(i.e parameter) also has name as 'a'.So,the local variable inside the method shadows(hides) the instance variable 'a' and in this example we are not able to set the value of instance variable 'a' as 5 because it has been shadowed by local variable 'a'.In the method setA() all 'a' means the loval variable 'a'.
We can solve this problem using the keyword 'this'.If we change the code at line 6 to:
6. this.a=a;
then the value of instance variable 'a' is set equal to the value of local variable 'a' i.e 5.This happens because we wrote 'this.a=a'.Its a way of saying the compiler that "set the current object's 'a' to local variable 'a'.But local variable 'a' has the value 5.So,the instance variable 'a' is set to 5 " .Here the current object is the object which was created on line 13.Since'we invoked the method setA() using the reference of the object created on line 13 so that object's instance variable 'a' is referred when we write this.a on line 6.So,'this' refers to the object on which the method is invoked.
I hope you are clear.
'this' has some other uses also like, this() means the current object's constructor.But you can understand those things once you understand what the keyword 'this' means.Remember,this always refers to the current object i.e the object on which the work is being currently done i.e the object on which the method is invoked.
Re: Java program without main method
[COLOR="red"][/COLOR][FONT="Georgia"][/FONT]class WithoutMain
{
static
{
System.out.println("This is a Program without a main method.");
System.exit(0);
}
}
Re: java program without main method
I ran this program. Wow amazing. It ran successfully. But I just want to know how it works internally?
Re: Java program without main method
The program wont run. Shows the following error.
Error: Could not find or load main class WithoutMain
Re: java program without main method
There is no magic behind this. static block gets called when ClassLoader tries to load the class.after loading the class only jvm calls the main method. so no wonder it executes the statements in the static block with out main method.
Re: java program without main method
Hi neelim ,
The program you provided will compile successfully,but will not execute due to main not found exception.