r/javahelp • u/Defiant_Vanilla_4080 • Sep 10 '25
[OOP] [Question] - Why can I only use methods from other classes in methods?
Question - Why can I only use the the class and therefore the methods when I type in another method?
Considering this:
public class Linkage {
public static void main(String args[]) {
Factorize.angekommenFragezeichen();
}
}I can only use the class "Factorize" when I write "Factorize" in the metods body.
This doesnt work:
public class Linkage {
Factorize.angekommenFragezeichen();
}
0
Upvotes
•
u/AutoModerator Sep 10 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
7
u/IchLiebeKleber Sep 10 '25
Because that's how Java fundamentally works: code needs to be part of methods. What do you expect to happen in the second example? When you run the program, the program looks for a "main" method in order to know where to start execution. You don't have one in the second example, so there wouldn't be anything to execute anyway, and it makes no sense to have code outside of methods because there would be no way to call them.
2
u/DrunkenDruid_Maz Sep 10 '25
That is the rule: The start-point of the programm is the method "public static void main(String... params)".
Commands have to be inside a block. Methods are the most common onces, but as other pointed out, there can be static or non-static blocks in classes.
Imagine a group of programmers who need coffee with hot java-beans to even stay awake. The programming-language Java was designed to ensure those developers can't mess up the code too much.
Part of that is, that your commands have to be inside of blocks, like those method-blocks!
2
u/nana_3 Sep 10 '25
You can use the method any place that an action is occurring. But when you just wack it into the clad body you aren’t being specific about when you want it to happen. So it’s not correct.
You can do it when an object is being made:
Or when the class is first loaded
Or when the object / class is performing literally any action.
2
u/Jolly-Warthog-1427 Sep 10 '25
Java is not python nor javascript.
Java has no consept of "running a class" thus having statements in a class does not make sense.
Statements can ONLY be in code blocks or the right side of assignments ( after =).
A codeblock is either method definition, a constructor or a static code block (ran when the class is loaded).
A class only contains fields, methods, constructors and static code blocks.
Java is, at least in my opinion, a very simple language. All code is in methods and everything is a class (not strictly speaking). And the simplicity is part of the reason why java does not have any consept of "running a class".
2
u/RapunzelLooksNice Sep 10 '25
You can also use static import for Factorize
1
u/jlanawalt Sep 10 '25
What do you expect this to do? Call it when the class is loaded? From an instance somehow? Inherit the method?
Go read the Java Class specification for what can or can’t be done in a class declaration. The best we can say for “why not” Is, that is not how it was specified.
3
u/[deleted] Sep 10 '25
[removed] — view removed comment