简体   繁体   中英

How do i call a class in the main method java?

OK im kinda new in java and i made this averaging program in one class, but now if i want to like call it from another class, then how do i do it?I tryed some object stuff but its hard to understand for. this is the code and i want this program to start when i call it.

    package Gangsta;
import java.util.Scanner;

public class okidoki {
    public static void main(String []args){
        Scanner input = new Scanner(System.in);

        double average, tests, grades, total = 0;
        int cunter = 1, start = 0;

        System.out.println("Press 2 to start averaging, or press 1 to end");

        start = input.nextInt();
        while (cunter<start){
        System.out.println("Enter how many tests u have");
        tests = input.nextDouble();

        System.out.println("Enter tests grades");
        int counter = 0;
        while (counter<tests){
            counter++;
            grades = input.nextDouble();
            total = grades+total;
        }
            average = total/tests;
                System.out.println(average);
        System.out.println("Press 3 to end or 1 to average again");
        cunter = input.nextInt();}

    }

}

this is the code where i want to execute it

    package Gangsta;

public class tuna {
    public static void main(String []args){
        okidoki okidokiObject = new okidoki();
        System.out.println(okidokiObject);
    }

}

In java the main method is the main (it's in the name) entry point of a program. That said, there is only one main in your program. Nevertheless if you want to have your code wrapped in another class just do it:

public class MyClass {
    public void myFancyMethod() {
        Scanner input = new Scanner(System.in);
        //....rest of
        //....your code
        counter = input.nextInt();
    }
}

and access it like:

MyClass myClassObject = new MyClass();
myClassObject.myFancyMethod();

You should really start reading (or read them again) the fundamentals of object oriented programming languages, naming conventions etc, because this is something you should understand to make progress in programming.

Object-Oriented Programming Concepts in Java

For now, you can just do this in tuna.java to achieve what you want:

package Gangsta;

public class tuna {
    public static void main(String []args){
        okidoki okidokiObject = new okidoki();
        okidokiObject.main()
    }

}

System.out.println(okidokiObject) prints Gangsta.okidoki@659e0bfd because it is the hashcode of your object (Hashcode is something like an ID, See Object toString() ). You usually do not want to print objects, but invoke their methods.

If you change your main method in okidoki class to constructor it will work exactly like you wish !

Example:

Example.java

public class Example {
    public Example() {
      System.out.println("Example class constructed");
    }
}

Main.java

public class Main {
  public static void main(String[] args) {
    System.out.println("Program started.Constructing Example class");
    Example exClass = new Example();
    System.out.println("Program finished.");
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM