Okay.. so, this may sound stupid. I have two classes. Foo.class, and Bar.class
Foo.class' Code:
public static void main(String[] args){ startBar(); }
Bar.class' Code:
public static void main(String[] args){ echo "HI"; }
I want the output to be "HI".. As in, i want to start my secondary class inside the first one.. I am probably doing a horrible job at explaining this. Things to keep in mind if you do understand what i want, i don't want a process builder that starts the bar.class.. I need it to all run in the one thingy..
Foo class
public static void main(String... args){
Bar.main(args);
}
Bar class
public static void main(String... args){
System.out.println("Hi");
}
Related
I keep getting this error and don't know what to do. It's frustrating the heck out of me:
Main method not found in class temperature.Temperature, please define the main method as: public static void main(String[] args)
Source code:
public class Temperature {
// ... various methods and properties
}
public class Test {
public static void main(String[] args) {
Temperature iceC = new Temperature();
Temperature iceF = new Temperature('F');
// ... etc
}
}
I guess you are trying to start your program by command line
with "java Temperature"?
So Java is searching the main method in the class Temperature.
Ways to go:
if you move your main method to the Class Temperature
if you move your inner class to an extra file and start your program with "java Test"
I am a first year comp sci student and going through my first round of textbook problems, all dealing with the System.out.println method (no, im not looking for help on homework problems. i've satisfied (as far as i know right now) the requirements of the problem, am just looking to gain some extra information).
The first problem asked me to write a program that would output this:
//////////////////////
|| Victory is mine! ||
\\\\\\\\\\\\\\\\\\\\\\
This was no problem. I wrote the following code:`
public class Stewie {
public static void main(String[] args) {
line();
qoute();
line2();
}
public static void line() {
System.out.println("//////////////////////");
}
public static void qoute() {
System.out.println("|| Victory is mine! ||");
}
public static void line2() {
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
}
}`
Later in the text, it asks me to write a program that prints the above figure 5 times in a row. This is also not a problem, I just rewrote the code from the first problem, like this:
/*
*/
public class Stewie2 {
public static void main(String[] args){
newStewie();
newStewie();
newStewie();
newStewie();
newStewie();
}
public static void newStewie() {
line();
qoute();
line2();
}
public static void line(){
System.out.println("//////////////////////");
}
public static void qoute(){
System.out.println("|| Victory is mine! ||");
}
public static void line2() {
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
}
}
That's all good and well, but I'd like to know how to import class Stewie from the first problem so I could use that in the second one without having to rewrite all the code. Can anyone help me out??
edit:re:importing a custom java class. I saw this before posting, but probably do not know enough about programming for it to be helpful to me at the moment. thank you though.
Have a look on how to create and use packages.
Because your methods in your first class are static declared, you can just call them within your second class.
package mypackage;
public class Stewie
{
public static void main(String[] args)
{
line();
qoute();
line2();
}
public static void line()
{
System.out.println("//////////////////////");
}
public static void qoute()
{
System.out.println("|| Victory is mine! ||");
}
public static void line2()
{
System.out.println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
}
}
The seconds class should also be in the same package. Then, because your methods declared in the first class are static, you can directly call them to your second class.
package mypackage;
public class Stewie2
{
public static void main(String[] args)
{
Stewie.line();
Stewie.qoute();
Stewie.line2();
}
}
Now in your second class, you can extend your code with further functionality.
Note: As already mentioned, this is allowed because your methods are static.
If your method, bellow wasn't static:
public void qoute()
{
System.out.println("|| Victory is mine! ||");
}
You would get the following error message:
Non-static method 'qoute()' cannot be referenced from a static context.
So be-careful and notice the difference.
This is a total beginners question. I am new to java and have been browsing StackOverflow and CodeReview. I am finding these two different formats being used:
example 1:
public static void main(String args[])
or
example 2:
public static void main(String[] args)
This is what I have in my course notes:
These words are called modifiers. The main() method is also preceded by the word void to indicate that it does not return any value. Also the main() method always has a list of command line arguments that can be passed to the program
main(String[] args)
which we are going to ignore for now.
So, as you can see, we have been told to ignore this for now, but I'd like to know:
Is there an actual difference between these, if so, what?
From the Java Language Specification:
The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.
For example:
byte[] rowvector, colvector, matrix[];
This declaration is equivalent to:
byte rowvector[], colvector[], matrix[][];
Actaully there are no difference between two main-method defination and both are correct.
But by convention java prefers array declaration as String[] args rather than String args[].
So it is more conventional -
public static void main(String[] args){...}
main method accepts arguement in String array
following ways are accepted
public static void main(String args[])
public static void main(String []args)
public static void main(String... args)
All these are valid declarations of the main function in Java.
public static void main(String[] args) {
// code
}
static public void main(String[] args) {
// code
}
static public void main(String args[]) {
// code
}
public static void main(String[] MarkElliotZuckerberg) {
// code
}
public static void main(String... NewYork) {
// code
}
The keywords public and static are interchangeable but mandatory.
The parameter of the main method can take the var-args syntax.
The name can be anything..!
These are examples of invalid main method declarations -
static void main(String[] args) {
// public is missing
}
public void main(String args[]) {
// static is missing
}
public static int main(String... Java) {
// return type not void
return 0;
}
public void Main(String args[]) {
// "main" not "Main"
}
public void main(string args[]) {
// "String" not "string"
}
public void main(String.. SayHi) {
// Ellipses is 3 dots !
}
I'm sorry if the source code is not soo readable... I always had trouble posting source code... :P ... Hope this helps...! If it did, let me know by commenting..!
Source - Java Tutorials on Theory of Programming
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I how many different ways can we declare a main method in java?
class A{
public static void main(String args[]){
System.out.println("hi");
}
}
Now I want different ways to create a main method. Could you explain me?
public static void main(String[] argument)
public static void main(String argument[])
public static void main(String... args)
public static synchronized void main(String... args)
public static strictfp void main(String... args)
public static final void main(String... args)
From the Java Documentation there are only two ways:
public static void main(String[] args)
and
public static void main(String... args)
The multiple ways of declaring the main method is (As everyone explained above)
public static void main(String[] args) or public static void main(String args[])
public static void main(String... args).
The positions of public and static may change as the programmer wish. But remember void should always come before main method. You can also use any parameters for the main method but the main with String[] args will only be executed first.
You can also execute a java program without a main method. For this you need to make use of static block with a break statement at the end.
Look at the methods below. Which ones will not compile? Which ones will compile, but can’t be used as entry points into an application? Which ones compile and act as you would expect a main method to act?
if any doubts in this regard please verify the following link
http://rationalpi.wordpress.com/2007/01/29/main-method...
you can also do this
static public void main(String args[])
There are two possible ways
By using single argument
public static void main(String args) { .. }
Or by varargs
public static void main(String... args) { .. }
Keep in mind that args in (String args) is just a argument name.You can use anything here like (String abc) , (String myargs) etc.
One last thing is that ,you can also pass a multi dimensional array from main like this
public static void main(String[][] args) { .. }
You can use var-args instead of array:
public static void main(String... args) {
Also this
public static void main(String... args)
I am learning core Java and I have one question, "Which is the first method called when the program is executed?"
That would be the main method.
It should be declared as
public static void main(String[] args)
It needs to be public, since the JVM should have access to call the method.
It needs to be static, as no objects are instantiated when the program starts
It takes an array of Strings as argument (coming from the command-line)
Some good links to have a look at:
The main Method (from the official Getting started trail)
What is the main method
Entry point for Java applications: main(), init(), or run()?
Some people may recommend you to write
public static void main(String... args)
this is equivalent to String[] args if you're using a compiler of version 1.5 or later. (I would discourage this unless you're extensively calling your main method internally with a varying number of arguments.)
Others may suggest
public static void main(String args[])
This is also equivalent, but discouraged by the Java Coding Convention.
It's usually main. But in this program, it's pain:
public class WhatThe {
public static final int x = pain();
public static int pain() {
System.out.println("pain!");
return 0;
}
public static void main(String[] args) {
System.out.println("main");
}
}
As it is in this one:
public class WhatThe {
static {
pain();
}
public static void pain() {
System.out.println("pain!");
}
public static void main(String[] args) {
System.out.println("main");
}
}
This is unlikely to be useful knowledge, but it's something to be aware of.
public static void main(String ar[])
Java programs start executing at the main method, which has the following method heading:
public static void main(String[] args)
public static void main(String... args) //java 1.5+
public static void main(String args[])
Read more at docs
In addition to aioobes answer
A usual way to start a simple java program is to execute java like this:
java com.example.MyClass
com.example.MyClass (or your fully qualified class name) needs to have a main method with exactly this signature:
public static void main(String[] args)
(you're only allowed to change the name of the parameter, like arguments instead of args). The virtual machine will try to load the named class and try to invoke this static method which will "start the Java program".
The main method will be called first,control goes to main method first which has the method headings:
public static void main(String[] args) or
public static void main(String args[])