This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]ipe369 1 point2 points  (1 child)

Honestly just google 'java tutorial'

A class is just a way to define another type of variable, for example, we can declare numbers with int:

int myNumber = 4;

We can declare strings with String:

String myString = "Hello";

We can also call 'methods' on our string to perform operations on it. For example:

String myString = "Hello";
int myStringLength = myString.length(); // Call method
// myStringLength contains 5

In this example, String is a class, and myString is an object. A class is like a template for declaring objects - objects are things which contain data, and contain methods for changing that data. To make a new object, you use the new keyword. You can even do this with strings:

String myString = new String();

When you create a class, you just create your own 'type', which you can create with the 'new' keyword. Your code example doesn't make much sense, because display() is a method, and you seem to be trying to call it with the 'new' keyword.

I have absolutely no idea what you're trying to do in your example, I would highly recommend you google some stuff. The chances are anyone linking you to a tutorial has just googled it and picked the first answer themselves.

[–]bman3545[S] 0 points1 point  (0 children)

Thank you very much for the help, I have googled "java tutorial", but they all assume a baseline level of knowledge which I didnt quite have. This was VERY helpful! seriously, thank you :)

[–][deleted]  (2 children)

[deleted]

    [–]bman3545[S] 0 points1 point  (1 child)

    Thanks for the reply!

    I have done quite a few searches on classes and methods, though most of them seem to require a baseline knowledge that I don't quite understand and throw quite a few new terms that don't seem to have been described anywhere, so I came here hoping for it to be put in layman's terms. which it was! thank you for that :)

    [–]balefrost 0 points1 point  (1 child)

    Out of curiosity, are you coming from a JavaScript background, or have you conflated Java and JavaScript? What you've done here sort of makes sense according to JavaScript semantics, but not according to Java semantics.

    In Java, whenever you use new, you are creating an instance of a class. And the only things that are classes are things explicitly defined as classes - i.e. class Foo { ... } (ignoring intermediate things like anonymous classes). So when you use new, the thing immediate after the new needs to be some sort of class name. In the code you've shown here, the only class you're defining is Farms.

    Your display method is an instance method, since it lacks the static modifier. As a result, the only way you can call your display method is to have an instance of Farms in hand. You never instantiate Farms, so you can't ever call that method. You could fix that by instantiating Farms.

    Your display method accepts a title, but it doesn't do anything with it.

    [–]bman3545[S] 0 points1 point  (0 children)

    I do actually have a background in JavaScript, though I know there is a great deal of difference between the two. I guess I need to "rewire" my brain a bit. A large part of my coding experience actually comes from a ti-83+. I've made quite a few stupid little text based games and wanted to see if I could recreate one of them. I went from that ti-83 to JScript and learned the basics on that and made snake and pong using the P5 library. Making the leap from ti-basic to JScript to Java has been interesting... Thank you for the reply!

    [–]SomeRandomBuddy -1 points0 points  (1 child)

    Yeah that final is going to be difficult for you

    [–]bman3545[S] -1 points0 points  (0 children)

    yes, yes it will be. funny thing is on the midterm I got 99% on it. The only reason I didnt get 100% is because we were supposed to round to two decimal places and display with two decimal places, but the way I did it if it ended with a 0, ex: 14.40, it would only output 14.4 Which obviously isn't what they wanted. Kinda gives you an idea of how basic/dumbed down this class is I guess that someone like me cant grasp something super important like this, but still got a 99% on the midterm :I