#Lets Discuss Fundamental Java with Shakiz

1 messages · Page 1 of 1 (latest)

lament widget
#

@past herald

past herald
#

yes

lament widget
#

lets continue here

past herald
#

1 sec

#

a method is like a

#

idk how to explain

dusky sigil
#

Im glad to help out if you have a quick question and conclure isnt here

past herald
#

oh hey

#

ty

lament widget
#

okay shakiz

#

what do you think of when I say eat

buoyant harbor
#

big boy

past herald
#

a method is the like what you do with an object ?

buoyant harbor
#

show an example with an explanation

past herald
#

like putting on a shirt ?

past herald
#

object is the plate

lament widget
#

maybe

past herald
#

and the act of eating it is the method

#

o.o

lament widget
#

write down the steps of eating

#

(generally)

buoyant harbor
#

what about the food?

lament widget
#

doesnt have to be perfect

past herald
#

-pick up the spoon
-put the spoon inside the food (idk how to describe this LOL)
-etc

#

is that what you meant ?

lament widget
#

yeah

#

could thats enough

#

in java we might be able to create an arbitrary representation of that

#
void eat() {
  spoon.pickUp();
  Food food = new Food("pizza cake");
  food.accept(spoon);
}
past herald
#

will this code actually work ?

lament widget
#

no

#

but its just a textual example

past herald
#

oh yeah i get it

lament widget
#

so back to your example

lament widget
#

whenever I say eat

#

you'd in principle, go through all those steps right?

past herald
#

yes

lament widget
#

yeah

#

now in java

#

we could simple do smtng like:

void eat() {
  spoon.pickUp();
  Food food = new Food("pizza cake");
  food.accept(spoon);
}

@Override
public void onEnable() {
  eat();
}
#

of course that'd be inside a class

#

but whenever you use eat()

#

you'd in reality, go through all the instructions of the function/instruction eat, which would be line 2 to 4

past herald
#

yes and that's what am trying to do on the plugin

lament widget
#

yeah

#

now let me ask

#

do you prefer
1 or 2?

1:

void eat() {
  spoon.pickUp();
  Food food = new Food("pizza cake");
  food.accept(spoon);
}

@Override
public void onEnable() {
  eat();
  eat();
  eat();
}

2:

@Override
public void onEnable() {
  spoon.pickUp();
  Food food = new Food("pizza cake");
  food.accept(spoon);
  spoon.pickUp();
  Food food = new Food("pizza cake");
  food.accept(spoon);
  spoon.pickUp();
  Food food = new Food("pizza cake");
  food.accept(spoon);
}
```?
past herald
#

1

#

cuz way more simple and easier to read

lament widget
#

exactly

#

thats good enough of a reason to prefer methods

#

(of course there are others as well)

#

so lets go through classes

past herald
#

yes

#

arent they the same thing ?

lament widget
#

methods and classes are very different

past herald
#

but like a class is a method + other things

lament widget
#

yes

#

a class usually contains variables and methods

#

but they're nonetheless different

past herald
#

so like a class groups all the methods

#

?

lament widget
#

hmm well

past herald
#

and they can have shared variables

lament widget
#

let me show an example of a class

#
class Shakiz {
  void eat() {
    System.out.println("Shakiz is eating, yum yum!");
  }

  void drive() {
    System.out.println("Shakiz is driving, don't distract him!");
  }
}```
dusky sigil
#

a class can be something like "bottle". you can make a new bottle and fill it using methods. basically anything you would call an object could be a class

lament widget
#

Shakiz

#

have you thought of that you can write something like:
Shakiz variableName = new Shakiz();

#

?

#

or well even simpler

#

String x = "a";

past herald
#

like

#

idk why

#

i have read some threads about it because i was confused

#

but didnt understand it

lament widget
#

did you ever figure out why:

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  String x = "a";
  Player player = (Player) sender;

  //so have you ever thought why you can do this:
  player.getName();

  //but why you cannot do this:
  x.getName();
}```
#

you can even try that code in one of your commands

past herald
#

right ?

lament widget
#

nope

past herald
#

:o

lament widget
#

x also references an object

#

a string object

#

so why is it that we can't run x.getName();

#

give it one more try

past herald
#

umm

lament widget
#

well

#

try add a class

#

called Shakiz

past herald
#

wait

#

so like

#

Player is a method/class ?

lament widget
#

is it a class or a method?

past herald
#

intensive confusion

#

CLASS

#

!

lament widget
#

uyes

past herald
#

also is String

lament widget
#

correct

past herald
#

but String has no method

lament widget
#

it does

past herald
#

named .getName();

lament widget
#

yes

#

thats correct

#

now I want you to try something

past herald
#

pogchamp

lament widget
#

create a class called Conclure

past herald
#

yes

dusky sigil
lament widget
#

now try this in your onCommand:

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  String x = "a";
  Player player = (Player) sender;
  Conclure conclure = new Conclure();

  player.getName();
  x.getName();

  conclure.getName();
}```
past herald
lament widget
#

if you added a class called Conclure

#

line 5 should work

#

(assuming you import it)

#

however the second to last line should give you an error right

past herald
lament widget
#

yeah

past herald
lament widget
#

now go back to Conclure

#

yuh

#

and put

#
public String getName() {

}
#

what happens?

past herald
#

it fixes the error

#

but does nothing

buoyant harbor
#

You teaching shakiz on how to make a constructor?

lament widget
#

yeah shakiz

#

now notice

#

if you turn it

public static String getName() {

}
#

what happens?

past herald
#

error

#

idk why this time

#

wait nvm

#

am just stupid

lament widget
#

well

past herald
#

no error

lament widget
#

you should get an error at conclure.getName()

past herald
#

but it didnt change anything ?

#

uhh

dusky sigil
#

hover over the getName with your mouse

#

it should give you a description of whats wrong

past herald
lament widget
#

oh yeah forgot about shitty Java

dusky sigil
#

the method is static, which means you can access it over the class name if the class has been imported

#

so it tells you you wouldnt wanna access it over the object

lament widget
#

yeah

dusky sigil
#

so if you capitalize the first 'C' in conclure it works

past herald
#

yes that's what i did

#

and it worked

lament widget
#

Conclure.getName() would refer to that you run the method from the class

past herald
#

so like this line isnt needed anymore ?

lament widget
#

anyways shakiz lets sum up this a bit now

lament widget
#

we're not done yet

#

so we can say

past herald
lament widget
#

that variables seem to have methods depending on the class you determine them of

#

for instance

#

String x = "a";
x.replace("a","b") works on x

#

because x has the type String, and in the class String.java a method replace() is declared/defined/listed

#

same goes for Player

#

it has the method getName()

past herald
#

but String doesnt

lament widget
#

exactly

#

which is why it yields an error

#

from now on

#

I will refer to static methods as functions

#

and normal (non static) methods as simply methods

#

shakiz, let me now introduce you to types

#

of course we can see String.java as a class right

#

because it literally is a physical class that exists within java

past herald
lament widget
#

we'll come back to that later

past herald
past herald
lament widget
#

shakiz, let me now introduce you to types
of course we can see String.java as a class right
because it literally is a physical class that exists within java

#

you agree on this? right?

past herald
#

yess

lament widget
#

however

#

String can also be seen as a type

#

and if we know that the variable x is of type String, then we also know what methods the type String has

#

which is replace, replaceAll, toString, hashCode... etc

#

we even tried with creating our own type Conclure

#

so what are classes more than just classes?

past herald
lament widget
#

can we call a class something else?

past herald
#

well if a String is a class and a type then yes ?

lament widget
#

yup

past herald
#

so every class could be a type ?

lament widget
#

yes

#

every class is a type

#

now there are some rather few exceptions

#

namely int, boolean, long, double, char, byte, short, float

#

as you know

#

its fully possible to to write any of these:

int x = 1;
double y = 2.2d;
float f = 321.423f;
short s = 1;
char c = 'a';
byte b = 1;
boolean a = true;
#

however have you ever thought that there are no methods to call on a

#

or x for that part

#

you cant write
x.getName()

#

or x.toString()

past herald
#

yeah now that i think about it

lament widget
#

thats because int, boolean, long, double, char, byte, short, float are all primitive types

#

they have no class

past herald
#

ok i get it

lament widget
#

yeah lets go back to your original problem

#

I think its enough for today

#

we can continue tomorrow

past herald
#

ok

lament widget
#

post it there

past herald
#

so i have to change that from String ?

lament widget
#

Lets Discuss Fundamental Java with Shakiz

past herald
#

pog

#

import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

public class CustomChat extends JavaPlugin {
    @Override
    public void onEnable() {

        this.saveDefaultConfig();
        this.getServer().getConsoleSender().sendMessage("§DCustomChat have been Loaded!");

    }
    @Override
    public void onDisable(){
        this.getServer().getConsoleSender().sendMessage("§DCustomChat have been Disabled!");

    }
    public static String getprefix(CustomChat plugin){

        String prefix = plugin.getConfig().getString("prefix");
        return prefix;
    }

}

import online.shakiz.CustomChat.CustomChat;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

public class PrefixTest implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        CustomChat customChat = new CustomChat();
        sender.sendMessage(CustomChat.getprefix(customChat));
        return true;
    }
}
#

hey , so i was learning for my test due tomorow and had my laptop open then i found fix for the code i showed before.

#

tried to use what conclure showed before

#

now this will be very useful cuz i can create multiple utils and use them on the code

#

and i also kind of see why use capitalized letters in start

#

of classes

past herald
#

:o i just did it

#

damn what conclure said makes sense now

#

i just made the CustomChat => customChat and removed "static" from the class

dusky sigil
#

Conclure is good at Java. of course it makes sense 😄