You should capitalize names of your classes. After doing that do this in your school class,

Classroom cls = new Classroom();
cls.setTeacherName(newTeacherName);

Also I'd recommend you use some kind of IDE such as eclipse, which can help you with your code for instance generate getters and setters for you. Ex: right click Source -> Generate getters and setters

Answer from ant on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [java]- calling on a method from another class
r/learnprogramming on Reddit: [Java]- Calling on a method from another class
June 21, 2015 -

I am new to Java, and am currently studying how to create packages with related classes in Eclipse.

If I create one class (example: Circle) defining the methods ( writing()) I want to use, and another class using those methods, do I have to include a reference to an instance of that class? Here is a code example of what I mean:

public class Circle{ 
    double r; 
    void wirte() { 
         Syste.out.println("Hello");
    }  

and in the other class:

public class Circle2{ 
     public static void main(String[] args) { 
          write(); 
  }} 

Would that work? Or do I always have to follow that format: classinstance.methodname()?

๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ how to call a method in another class from the main class?
r/javahelp on Reddit: How to call a method in another class from the main class?
April 20, 2023 -

UPDATE

I did not know I can do something like this

public class GUI(){
//do something
}

public static void main(String[] args){
   new GUI();
}

This code block worked for me --- thank you for pointing it out.

---------

So my IDE flagged an error at this line in main (from my main class Tic_Tac_Toe)

public static void main(String[] args) {

        ColRowLists ttt = new ColRowLists();
        char[][] board = new char[3][3];
        System.out.println("Welcome to Tic Tac Toe");
        userInput(board);

        System.out.println("show me your list" + ttt.winOrLose()); //"'winOrLose(java.util.List, java.util.List, java.util.List, java.util.List, java.util.List, java.util.List, java.util.List, ...)' in 'ThirtyMinutes.ColRowLists' cannot be applied to '()'"


    }

The winOrLose method in question lies in another class ColRowList: https://github.com/morry239/TTT_experiements/blob/master/src/ThirtyMinutes/Tic_Tac_Toe.java

I tried to do something like this, but it did not work obviously:

System.out.println("show me your list" + ttt.winOrLose(List topRow, List midRow, List botRow, List leftCol, List midCol, List rightCol, List diagonal1, List diagonal2);

Could anyone kindly point me in the right direction? My main class is here FYI.

๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_class_methods.asp
Java Class Methods
To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon (;). A class must have a matching filename (Main and Main.java). Like we specified in the Classes chapter, it is a good practice to ...
๐ŸŒ
Team Treehouse
teamtreehouse.com โ€บ community โ€บ calling-method-from-another-class
Calling method from another class (Example) | Treehouse Community
January 26, 2015 - I'd also avoid using the name main for a method because if it has the signature: ... It will be treated as the entry point of the application. Assuming you're trying to build something like a quiz app, here's a simple data model for a question that you could start with: public class Question { private String mPrompt; private String mAnswer; public Question(String prompt, String answer) { mPrompt = prompt; mAnswer = answer; } public String getPrompt() { return mPrompt; } public String getAnswer() { return mAnswer; } }
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ how to call a method in another class in java
How to Call a Method in Another Class in Java | Delft Stack
February 2, 2024 - To class a method of another class, we need to have the object of that class. Here, we have a class Student that has a method getName(). We access this method from the second class SimpleTesting by using the object of the Student class.
๐ŸŒ
JavaBeat
javabeat.net โ€บ home โ€บ how to call a java method from another class?
How to Call a Java Method From Another Class?
March 20, 2024 - Now create an instance of the JavaBeat class in the main() method of the JavaDeveloper class. After this, use this object with the dot syntax to call the empSal() method of the JavaBeat class: In this example, we create two classes in two different packages and access a public method of one class into another.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 706451 โ€บ java โ€บ Call-method-class
Call method from other class (Beginning Java forum at Coderanch)
Because that's how Java works. public static methods can be called from anywhere by supplying the name of the class that the method is defined in. So the code above says: "Call the static method updateCountDownText() defined in class MainActivity" HIH Winston ยท But like you say the method ...
Find elsewhere
๐ŸŒ
Team Treehouse
teamtreehouse.com โ€บ community โ€บ how-do-you-call-the-methods-of-a-class-that-was-created-in-another-class-from-another-class-java-swing-android
How do you call the methods of a class that was created in another class, from another class? Java, Swing, Android (Example) | Treehouse Community
April 24, 2015 - ... If the classes are in the same package, then you can just initialize a new instance of an object from the class you want to call the method from in the class you want to call the method in e.g.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-call-private-method-from-another-class-in-java-with-help-of-reflection-api
How to call private method from another class in Java with help of Reflection API? - GeeksforGeeks
July 12, 2025 - We can do this by changing the runtime behavior of the class by using some predefined methods of Java. For accessing private methods of different classes we will use Reflection API. To call the private method, we will use the following methods of Java.lang.class and Java.lang.reflect.Method
๐ŸŒ
CodingNomads
codingnomads.com โ€บ how-to-call-static-nonstatic-methods-in-java
How to Call Static & Non-Static Methods in Java?
Inside a non-static method and calling a static method: just call the method with its class. ... Inside a non-static method and calling another non-static method: create an object and use it as a reference.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java-program-to-pass-method-call-as-arguments-to-another-method
Java Program to pass method call as arguments to another method
import java.util.Scanner; public class Main { public int my_sum(int a, int b) { int sum = a + b; return sum; } public void my_cube(int my_input) { int my_result = my_input * my_input * my_input; System.out.println(my_result); } public static void main(String[] args) { Main obj = new Main(); int my_input_1, my_input_2; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the first number : "); my_input_1 = my_scanner.nextInt(); System.out.print("Enter the second number : "); my_input_2 = my_scanner.nextInt(); System.out.println("The cube of the sum of two numbers is: "); obj.my_cube(obj.my_sum(my_input_1, my_input_2)); } }
๐ŸŒ
Quora
quora.com โ€บ How-do-I-use-method-from-another-Java-class
How to use method from another Java class - Quora
Answer (1 of 15): It depends on what behaviour you want and what access level there is. All below access modifiers can be used with methods (and not only :) Access Modifiers ( private, public, protected, default) Non-Access Modifiers (abstract, final, static, synchronized(thread safe), violate ...
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ how-to-call-a-method-in-java
How to Call a Method in Java: Guide to Executing Functions
March 6, 2024 - In this comprehensive guide, weโ€™ve explored the process of calling methods in Java, from the basics to more advanced scenarios. Weโ€™ve learned how to create an instance of a class, use the dot operator, understand method parameters, and handle ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 377430 โ€บ java โ€บ Calling-method-class
Calling a method in another class (Java in General forum at Coderanch)
How do I call the method when I don't have instances of that class in my other class? ... You can't (unless it's a static method; you don't need an instance to call a static method. ) People new to object-oriented language ask this question a lot. It takes a while to learn to think in objects. But let me ask you a few leading questions: how do you feed a baby? Answer: first, you get the food; then you get the baby; then you give the food to the baby.
๐ŸŒ
Liberian Geek
liberiangeek.net โ€บ 2023 โ€บ 12 โ€บ call-method-from-another-class-java
How to Call a Method From Another Class in Java | Liberian Geek
The โ€œmain()โ€ method is called ... To call/invoke a user-defined method from another Java class, create an object of the class containing the method and call/invoke the particular method via the created object....
๐ŸŒ
JanbaskTraining
janbasktraining.com โ€บ home โ€บ how to call a method in java?
How to Call a Method in Java from another or Same Class?
May 20, 2019 - By making use of Remote Method Invocation (RMI), one program can invoke the method of another over a network to get the desired output. One can access the files by calling methods from any kind of machine on the internet.
Top answer
1 of 6
41

You need to somehow give class Alpha a reference to cBeta. There are three ways of doing this.

1) Give Alphas a Beta in the constructor. In class Alpha write:

public class Alpha {
   private Beta beta;
   public Alpha(Beta beta) {
     this.beta = beta; 
   }

and call cAlpha = new Alpha(cBeta) from main()

2) give Alphas a mutator that gives them a beta. In class Alpha write:

public class Alpha {
   private Beta beta;
   public void setBeta (Beta newBeta) {
     this.beta = beta;
   }

and call cAlpha = new Alpha(); cAlpha.setBeta(beta); from main(), or

3) have a beta as an argument to doSomethingAlpha. in class Alpha write:

public void DoSomethingAlpha(Beta cBeta) {
      cbeta.DoSomethingBeta()
}

Which strategy you use depends on a few things. If you want every single Alpha to have a Beta, use number 1. If you want only some Alphas to have a Beta, but you want them to hold onto their Betas indefinitely, use number 2. If you want Alphas to deal with Betas only while you're calling doSomethingAlpha, use number 3. Variable scope is complicated at first, but it gets easier when you get the hang of it. Let me know if you have any more questions!

2 of 6
26

Method 1:

If the method DoSomethingBeta was static you need only call:

Beta.DoSomethingBeta();

Method 2:

If Alpha extends from Beta you could call DoSomethingBeta() directly.

public class Alpha extends Beta{
     public void DoSomethingAlpha() {
          DoSomethingBeta();  //?
     }
}

Method 3:

Alternatively you need to have access to an instance of Beta to call the methods from it.

public class Alpha {
     public void DoSomethingAlpha() {
          Beta cbeta = new Beta();
          cbeta.DoSomethingBeta();  //?
     }
}

Incidentally is this homework?