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"});
}
}
Related
Let us suppose we have a class:
package package1;
public class Car {
public static int brake()
{
int x;
//some functionalities
}
//other functionalities
}
I want to ask for using this method brake in classes of different package, do we need to include package name also? -- int xyz=package1.Car.brake(); or simply Car.brake(); will work
You can import package or use full path of method:
First solution:
public class App {
public static void main(String[] args) {
package1.Car.brake();
}
}
Second solution:
import package1.Car;
public class App {
public static void main(String[] args) {
Car.brake();
}
}
Add import statement like import package1.Car; and then you can use Car.brake(); to call the function. Read more about imports here
I have two java class file's in my project.
First one is main.Java
Second one is function.java
How to call function.java method from main.java
for Example.
main.Java
public class main {
//call function here
}
function.Java
public class function {
public void example(){
System.out.println("Function working");
}
}
How to call function.java example method from Main.Java?
Class name should be always start with Upper letter.
This is the way to call a function in another class in another .java
In Main.java
public class Main{
public static void main(String[] args){
SecondClass sc = new SecondClass();
sc.go();
}
}
In SecondClass.java
public class SecondClass{
public void go(){
System.out.println("Done");
}
}
If you want to call the method in the same class
public class Main{
public void go(){
System.out.println("Done");
}
public static void main(String[] args){
Main m = new Main();
m.go();
}
}
or this
public class Main{
public static void go(){
System.out.println("Done");
}
public static void main(String[] args){
go();
}
}
First make the example() method static
public static void example(){
System.out.println("Function working");
}
Then call
public class main {
//call function here
public static void main(String[] args) {
function.example();
}
}
if they are not in the same package you should import function.java in main.java like this
import function
then you should create an instance of function class like this where you want to use it
function func = new function();
then you can call the example method using func object you created like this
func.example();
Your method isn't static, so you need to instantiate object from this class(function) to obtain access to the example method. First of all read some java specific book, so you can understand what static keyword is and how you can use it.
The code below will work :
package main.Java; //this is from where you want to call.
public class function {
public void inFunction(){
function.Java.function function = new function.Java.function();
function.example();
}
}
Happy learning :)
This question already has answers here:
Writing a function inside the main method - Java
(7 answers)
Closed 6 years ago.
Hello I am a want create method into main?
So this is code where i want crete method:
import java.io.*;
import java.util.Random;
class input{
public static void main (String args[]){
void Randomises() {
int writabledata;
Random a=new Random();
writabledata=a.nextInt();
}
}}
Java does not allow to create methods within a method. This is a general rule and NOT specific to main method.
Perhaps what you are looking for is to create additional static methods in your class:
public final class MyCommand {
private MyCommand() {
//
}
private static void releaseTheCatsOfWar() {
System.out.println("Meow!");
}
public static void main(String[] args) {
releaseTheCatsOfWar();
}
}
Of course, it would be better to do:
public final class MyCommand {
private MyCommand() {
//
}
private void releaseTheCatsOfWar() {
System.out.println("Meow!");
}
public static void main(String[] args) {
MyCommand that = new MyCommand();
that.releaseTheCatsOfWar();
}
}
you cannot create methods within methods in Java.
You can have it as an additional method of your class and call it form main.
public static void main is not a class, it's a method within class. And your class is input (by the way, you should start class name with an uppercase letter, like Input), in which you define method main, from which your program starts working.
Although it is entirely pointless, the closest way you would get to achieving your results is with the following:
public class Main {
private static interface MethodMaker{
void randomise();
}
public static void main(String[] args) {
MethodMaker methodMaker = new MethodMaker(){
#Override
public void randomise() {
// DO SOMETHING
}
};
methodMaker.randomise();
}
}
You have to put method outside main ,, and just call it in main ,, like this :-
import java.io.*;
import java.util.Random;
class input{
public static void main (String args[]){
Randomises();
}
public void Randomises() {
int writabledata;
Random a=new Random();
writabledata=a.nextInt();
}
}
I have a question on class imports, It seems you can call a method with a reduced line if you have imported the class. I don't understand what is the name of this operation, and how is it possible...
For instance :
Why this code
public class test
{
public static void main (String args[])
{
System.out.print("Test");
}
}
Can be replaced by
import static java.lang.System.out;
public class test
{
public static void main (String args[])
{
out.print("Test");
}
}
What happens if you have also an object named "out" ?
Thanks in advance
What happens is that out from external class must be referenced by full name:
String out = "Hello World";
java.lang.System.out.println(out);
The variable out will shadow the static import and you will have to use the full name in order to use the function print.
import static java.lang.System.out;
public class Tester5 {
public static void main (String args[]) {
int out=0;
out.print("Test");
}
}
yields "cannot invoked print(String) on primitive type int. The same error is shown if out is an object.
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).