#Understanding something about Java

1 messages · Page 1 of 1 (latest)

steel pollen
#

Why is there this public void static main thing wrapped around the code?

But more confusingly to me, why are functions (or methods as they are called here in Java land) defined after calling them? I see the tutorials call the method name, but the actual method itself is written after the caller.

For reference I am used to Pascal where you write function ... and procedure... at the top of program and even though the function can be written anywhere in the program, this specific thing is written at the top of the program.

I hope someone can explain to me; I am new to this!

ivory novaBOT
#

<@&987246399047479336> please have a look, thanks.

craggy cape
#

The psvm is the entry point of the program. Not every class has to have a public static void main, just the one where you start the program. Order of functions/methods doesn't matter. Nor do class level variables. Inside the class Widget{ } brackets, it's a dumping ground, order stuff how you wish.

steel pollen
#

And then if I wanted to create front-ends for Java software, how does one go about doing this? I personally am used to the RAD design where you create the GUI in drag n drop and then write the code 'behind' it

I know as strange as it seems to me that a GUI written in fully text is actually more beneficial somehow.

craggy cape
#

Java is class based. I re-read your question. You're asking why someone has just a public static void main function that has all the code in it? That's generally not normal.

steel pollen
#

And through learning Java do I cover the stuff needed for Kotlin? I have a self perpetuating app that is maintained and built using Gemini (as a Kotlin/Java/C++ application), but I would love to know what it is doing lol, and be able to interject sometime!

craggy cape
#

I use psvm as just a shorthand for public static void main. In the IDE, that is the shortcut method to declare that function instead of typing it all out.

steel pollen
craggy cape
#

Yes, you are just looking at baby stuff, tutorial examples.

steel pollen
steel pollen
craggy cape
#

Intellij does have a gui builder for javafx projects. I advise against being reliant on them but to each their own.

#

How one would write a java program?

steel pollen
craggy cape
#

Just throwing code in a psvm function is one way to skin a cat but I think most people learn an OOP style and do something like.

public class Widget{
   int size; 
   String name;
   
   //constructor
   public Widget(String name, int size){
     this.name = name;
     this.size = size;
   }
   
   // override a default method of objects 
   @Override
   public String toString(){
     return "name: " + name + ", size: " + size;
   }
   
   // use the widget 
   public static void main(String[] args){
     Widget w1 = new Widget("fergal", 12);
     Widget w2 = new Widget("msfh", 4);
     System.out.println("Widget1 = " +w1);
     System.out.println("Widget2 = " + w2);
   }
}
#

So this is also example code. Ifyou had a real use for widgets it probably wouldn't be logical to have a program inside the class Widget. You might make a WidgetApp class and then have something like this psvm above in that class.

#

I suppose you can just uninstall intellij and reinstall the CE edition. Eclipse I don't think has a gui builder but Netbeans does.

#

I'm also not sure if the intellij CE edition has the gui builder. Someone else here will have to confirm that.

craggy cape
# steel pollen Oh I guess they do that just for the tutorial sake? Like it looks something like...

In this code example, there's a few things that might throw a person off. First the class name doesn't have to be Main. Why they do that is straight up stupid. The other thing, nothing is really using the class aspect in this code example. The entry point, public static void main, you'll notice, has the word static in it, which basically means global access. And the function you created, add is also static, and thus reachable from inside the main function.

#

In the OOP example, you'll notice, you have to create a new Widget.

glacial wolf
#

@steel pollen That's easy why. public just means it can be called outside the class methods. static means it can be called without instanciating the class. void because it returns no value and main is just the entry point name.

craggy cape
#

Then you would call methods defined in the Widget class.
w1.getFurbleGom() if you wanted to call the getFurbleGom method.

glacial wolf
#

Suppose you want a utility function that operates purely on the function's input arguments. In such a case, it'd make sense to make the method static since it would not rely on the this pointer/object. It does not need instantiating.

steel pollen
#

I will respond tomorrow you guys as i need to sleep
Thanks for your messages, im definitely not done needing help though, so please dont close the thread, or i may open a new one

split herald
rocky fjord
craggy cape
steel pollen
steel pollen
craggy cape
steel pollen
#

Also

void main() {
    IO.println("Hello, World!");
}
```Why does the IO part say it is undefined? Accordining to the guide, it should compile?
ivory novaBOT
viral totem
#

as it was added in that version

steel pollen
viral totem
#

are u using intellij?

#

"project structure"

#

that window has those settings

steel pollen
viral totem
#

maybe right click the project folder and then settings or something like that

viral totem
#

thats eclipse itself

#

not ur project

steel pollen
viral totem
#

right click ur project folder

#

it probably has Settings at the bottom

ivory novaBOT
#
TJ-Bot
Where in eclipse can i find the setting to change the jdk used by my project?

To change the JDK used by your project in Eclipse, follow these steps:

  1. Right-click on your project in the Package Explorer.
  2. Select "Properties" from the context menu.
  3. In the Properties window, navigate to "Java Build Path."
  4. Click on the "Libraries" tab.
  5. Select the JRE System Library entry and click on "Edit."
  6. In the JRE System Library window, you can choose a different JRE or JDK installed on your system.
  7. Click "Finish" to apply the changes.
  8. Click "Apply and Close" in the Properties window to save the configuration.

After changing the JDK for your project, make sure to clean and rebuild your project to ensure that it compiles correctly with the new JDK.

viral totem
#

^sounds good

steel pollen
viral totem
# steel pollen It says "unbound"

u can choose here to either use the same jdk that eclipse uses (execution environment) or to let this project use an alternative separate jdk (alternate jre)

in either case, u can configure the details in that "..." button to the right of it

#

and yeah, u see that ur running on 21 right now

#

instead of 25

steel pollen
ivory novaBOT
#

@steel pollen

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

steel pollen
viral totem
#

nice 👍

#

with java 25 u can even leave the class away

#

lmao

steel pollen
viral totem
#

so u also need to get rid of the package

#

and i dont know how well that plays with eclipse specifically

#

but yeah

#

as far as java 25 is concerned, this is a valid and complete program:

void main() {
  IO.println("Hello World");
}
craggy cape
#

Was there an error message? Could be Eclipse doesn't have their shit together yet.

viral totem
#

or module-info.java

craggy cape
#

Eclipse makes one for you?

viral totem
#

by default yes

#

its checked in the "new project dialog"

#

frequently running beginners into issues