why am I getting this error - java

Why Am I getting an error. In eclipse it says constructor call should be the first line. It is the first line. or you can not extend Main?
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame{
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//JLabel testLabel1 = new JLabel();
public Main(){
super("title bar");
}
}
}

Your Main constructor should sit outside of the main method. Like so:
public class Main extends JFrame {
public Main() {
super("title bar");
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//JLabel testLabel1 = new JLabel();
}
}

The constructor should be outside public static void main(String[] args) {
It's a function and you can't have a constructor inside a function .

You're trying to define a constructor (public Main) within a static method. That's not valid in Java.
You probably meant something more like this:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame{
/**
* #param args
*/
public static void main(String[] args) {
}
// The constructor isn't *inside* `main` anymore:
public Main(){
super("title bar");
}
}

There are multiple errors.
The first error is that you're defining the constructor of the class in a method. This is illegal which results in the compiler complaining that it expected the new keyword instead of public.
Secondly, super class methods have to be called in the first line. But since, the compiler is now confused due to the previous error, it has reported so.
You might also want to improve on the class naming convention. It is very easy to get confused between the main(String args[] method, which is the entry point into your code, the Main class, and its constructor Main() (which would generated by the compiler).

Related

Java question :instance of a class as a static member of this class

I am maintaining a Java program , I am not the author.
I came accros the following situation:
In the code below I do not the declaration
static MainWindow frame = new MainWindow();
What is the purpose of this statement?
See below the whole code
public class MainWindow {
static MainWindow frame = new MainWindow();
// constructors
public MainWindow() {
System.out.println("this is the constructor.");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("this is main method.");
}
}
Perhaps this was done in order to only ever have one instance of the MainWindow across the program, however in that case I would expect the constructor to be private and the static member to be public.

Static method not being called Java

Since the main method is static, it should be able to be called statically. Therefore, I use:
package prototype.server.main;
import javax.swing.JFrame;
import prototype.server.main.gui.Swing;
public class Runtime {
public static void main(String[] argv) {
Swing swing = new Swing(true, argv[0]);
#SuppressWarnings("unused")
JFrame maingui = swing.getGuiFrame();
}
}
as the static-main code, then call using:
import prototype.server.main.Runtime;
public class Main {
Runtime.main(new String{"f"});
}
to call the static method, but Eclipse is giving me an error. Please help and thank you in advance.
There are two bugs we need to fix;
import prototype.server.main.Runtime;
public class Main {
// add constructor, or method that you can call another method
// or make this static { ... } block that fits you
public Main() {
//do not forget [] for array
Runtime.main(new String[]{"f"});
}
}

How to access a method from another class outside the creator of the instance

I created 3 java files:main.java, GUI.java, Serial.java
In Main I created instances of two last java files.
I can call methods from gui and serial from within Main.
I cannot call methods from the instances gui and serial outside Main.
package main;
public class Main {
public static void main(String[] args) {
GUI gui = new GUI();
gui.setVisible(true);
Serial serial = new Serial();
serial.getPorts();
fillList();
}
public void fillList () {
gui.setList("hoi");
}
}
Why is this? How can I call methods from gui from method fillList?
Thanks for any insights.
The instances only exist within the method they're declared, in this case the constructor. A common way of getting around this is to declare a field in your class, and assign the value of that field in the constructor (or other method). Try:
package main;
public class Main {
// private GUI gui; // replaced this line with the below. See comment 5
private static GUI gui; // this is the new field declaration
public static void main(String[] args) {
gui = new GUI(); // note I removed the class declaration here since it was declared above.
gui.setVisible(true);
Serial serial = new Serial();
serial.getPorts();
fillList();
}
public void fillList () {
gui.setList("hoi"); // now this method has access to the gui field
}
}

Java - Calling a public method (not public void)

I have this code but I am unsure how to call the public create() method with the public static void main(String[] args) method in a different class. I have searched the net but found nothing on just public methods just public void methods.
Here is the code
mainclass.java:
public class Mainclass {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
create createObject = new create();
createObject.create();
System.out.println("dammit");
}
}
create.java
public class Create extends javax.swing.JPanel {
public create() {
initComponents();
setDate();
}
}
That is actually a constructor. You call it with new.
create panel = new create();
Having a class with a lowercase name is highly unorthodox.
That is a constructor, not a normal method. You can see that it has the same name as the class it's in. It's called here:
create createObject = new create();
BTW, calling a class create makes me a sad panda.
public create() {
initComponents();
setDate();
}
create() Is a constructor so when you say Create createObject = new Create(); the without parametrized constructor will automatically call.
read here for constructor
and please follow the java naming conventions class name always start with caps letter.
Starting with something Off-topic but you might want to read about the Java Naming Convention to understand how to name your classes/methods/variables in Java.
Constructors as we know have declarations that look like method declarations—except that they use the name of the class and have no return type. So you can have something like this:
public class create extends javax.swing.JPanel {
/** Normal Constructor */
public create() {
}
/**
* Normal method for initialization.
*/
public void create(){
initComponents();
setDate();
}
}
I tend to prefer having a specific method to handle object initialization (or setup, if you may) for my classes i.e. kept all business logic separate from object creation mechanism. So, if I were you, I would've renamed my method as public void doCreate() and initialized everything over there. (This is subjective and a matter of preference). With this case, my MainClass would have changed something like this:
public class MainClass {
/**
* #param args
*/
public static void main(String[] args) {
create createObject = new create();
createObject.doCreate();
System.out.println("dammit");
}
}

Is it possible to call the main method passing args[] from another method?

I'm trying to call the main method of a class from another method passing arguments like when running the class from the command line. Is there a way to do this?
You can call the main method as you would call any other (static) method:
MyClass.main(new String[] {"arg1", "arg2", "arg3"});
Example:
class MyClass {
public static void test() {
MyClass.main(new String[] {"arg1", "arg2", "arg3"});
}
public static void main(String args[]) {
for (String s : args)
System.out.println(s);
}
}
Yes, the main method can be called like any other method, so if you have a class Test with a main method, you can call it from any other class like:
Test.main(new String[] { "a", "b" });
and this way you'll pass "a" and "b" as the parameters.
Have you tried something like :
// In your method
String[] yourArgs = new String[] {"foo", "baz", "bar"};
YourClassWithMain.main(yourArgs);
But I think this is not a good idea, the main() method should only contain some very basic code which calls the constructor. You shouldn't call it directly, but rather create a new instance of your other class which will do all the initialization needed.
The answer is yes,
Since main is a static method and is public method, you can do this (and it compiled on my case):
/**
* #author The Elite Gentleman
*
*/
public class Test {
/**
*
*/
public Test() {
super();
// TODO Auto-generated constructor stub
Test.main(new String[] {"main"}); //Yes, it works and compiles....
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello");
}
}
Sure, you can call the main-method just as an ordinary (static) method like this:
TheClass.main(new String[] { "lorem", "ipsum" });
As a side note, you could declare the main method like this:
public static void main(String... args) { ... }
and call it like
TheClass.main("lorem", "ipsum");
The bytecode produced is the same (varargs are compiled to arrays), so it is backward compatible in all ways (except that it won't compile on non-vararg aware java-compilers).
You could just rename your main and make a new one, making it call the "new" main. At least that's what I generally do when unit testing

Categories