Would not let me run the application - java

package aaa;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class pear {
public class aaa extends Application{
#Override
public void start(Stage primaryStage){
Button okbt = new Button("ok");
Scene scene = new Scene(okbt, 200,250);
primaryStage.setTitle("n");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
Application.launch(args);
}
}
}
This is the an example of codes that my text book had for me and I try to run it but it wouldn't run. This is the error:
Error: Main method not found in class aaa.pear, please define the main method as:
public static void main(String[] args).
I don't understand why its wrong because the main class is outside of the start class and inside of the extends Application. This is directly from a book I just need to know why it won't run.

static methods can only be declared in a static or top level class rather than in a non-static nested class. In this case its simpler just to use the latter
public class MyPearApp extends Application {
public static void main(String[] args){
...
}
}

Your main method is in an inner class (aaa). Try putting it directly in the pear class instead.

Everything should be Inside main method:
For Example:
public class pear {
public static void main(String[] args) {
//Logic and Everything else goes in here
}

Related

using a static method in a different class

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

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 handle class's from Two Different .Java Files?

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 :)

Access Class methods without prefixing with class

In a program like:
package testing;
import MarcoLib.Mouse;
import MarcoLib.Timings;
public class Testing {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Mouse.pressMouse(1);
}
}
Is there a way to call Mouse.pressMouse() without prefixing using Mouse?
You can import static methods:
import static com.company.Mouse.pressMouse;
public static void main(String[] args) {
pressMouse(1); // No need to prefix with "Mouse."
}
If the method pressMouse is static, then you could Static Import your method(s),
import MarcoLib.Mouse;
import MarcoLib.Timings;
import static MarcoLib.Mouse.pressMouse;
public class Testing {
public static void main(String[] args) {
pressMouse(1);
}
}
and per the link,
So when should you use static import? Very sparingly!
If the method pressMouse isn't static, then you could extend Mouse,
import MarcoLib.Mouse;
import MarcoLib.Timings;
public class Testing extends Mouse {
public static void main(String[] args) {
new Testing().pressMouse(1);
}
}

Simple Java OOP Class Error

I'm trying to print my name using Java OOP Classes, Objects and Method. Below are the two scripts I'm using. Nothing is showing on the screen. I'm using Netbeans IDE.
package name;
public class Begin {
public static void displayName(){
System.out.print("Say My Name");
}
}
===========================
package usename;
import name.Begin;
public Useaname {
public static void main(String[] args){
Begin.displayName();
}
}
You're missing the class in your "Useaname" class declaration.
public class Useaname {
Where is Your Keyword class before your Username ?? :)
public class Useaname {
public static void main(String[] args) {
Begin.displayName();
}
}
You have to declare the method static in order to call it like Begin.displayName().
public static void displayName() {
This tells makes the method a function of the class as opposed to any particular instance of the class.
Alternatively, you can instantiate an object to call the method:
Begin begin = new Begin();
begin.displayName();
You have missed the keyword class before the class name Useaname. Here is the solution code.
package usename;
import name.Begin;
public class Useaname {
public static void main(String[] args) {
Begin.displayName();
}
}
This code should not compile.
Begin.displayName(); tries to invoke static method - you dont have such a thing :).
you have to ways - create new Begin object and invoke method on it - new Begin().displayName()
or declare displayName as static method.
There are at least two solutions to this problem:
(1) You can make the displayName() method static. This will allow you to call it with the class name.
public static void displayName() {
// snip
}
(2) You can create an object from your Begin class in order to call the non-static member method:
public static void main(String[] args){
Begin b = new Begin();
b.displayName();
}
To summarize, you should learn about objects and object creation as a beginning step to understanding Java and OOP in general.
You could use a static modifier on your displayName() method or create a new instance of your Begin class:
package usename;
public class Useaname {
public static void main(String[] args){
Begin begin = new Begin();
begin.displayName();
}
}
You haven't use the keyword "class" before "usename"
package usename;
import name.Begin;
public Useaname {
public static void main(String[] args){
Begin.displayName();
}
}
Correction
package usename;
import name.Begin;
public class Useaname {
public static void main(String[] args){
Begin.displayName();
}
}
you forgot class keyword ! and you can create object for class Begin
import name.Begin;
public class Usaname
{
public static void main(String []args)
{
Begin ob = new Begin();
ob.displayName();
}
}
You forgot to add class before Username
package username;
import name.Begin;
public class username{
public static void main(String[] args){
Begin.displayName();
}
}
What you are missing here is the class keyword before Username class declaration
Since you declared the displayName() method as static using the static access modifier , there is no need to create an object of the Begin class in order to invoke the displayName() method.
In general , this is how the Username class should be :
package usename;
import name.Begin;
public class Useaname {
public static void main(String[] args){
Begin.displayName();
}
}
Alternatively, you can invoke the displayName() using a class instance by creating an object of the Begin class, just like this:
package usename;
import name.Begin;
public Useaname {
public static void main(String[] args){
Begin b = new Begin();
b.displayName();
}
}

Categories