#Lets Discuss Fundamental Java with Shakiz
1 messages · Page 1 of 1 (latest)
yes
lets continue here
Im glad to help out if you have a quick question and conclure isnt here
big boy
a method is the like what you do with an object ?
show an example with an explanation
like putting on a shirt ?
or eating a plate
object is the plate
maybe
what about the food?
doesnt have to be perfect
-pick up the spoon
-put the spoon inside the food (idk how to describe this LOL)
-etc
is that what you meant ?
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);
}
will this code actually work ?
oh yeah i get it
so back to your example
this
whenever I say eat
you'd in principle, go through all those steps right?
yes
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
yes and that's what am trying to do on the plugin
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);
}
```?
exactly
thats good enough of a reason to prefer methods
(of course there are others as well)
so lets go through classes
methods and classes are very different
but like a class is a method + other things
yes
a class usually contains variables and methods
but they're nonetheless different
hmm well
and they can have shared variables
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!");
}
}```
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
Shakiz
have you thought of that you can write something like:
Shakiz variableName = new Shakiz();
?
or well even simpler
String x = "a";
no
like
idk why
i have read some threads about it because i was confused
but didnt understand it
yes yum yum
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
x is a string but player is an object
right ?
nope
:o
x also references an object
a string object
so why is it that we can't run x.getName();
give it one more try
umm
is it a class or a method?
uyes
also is String
correct
but String has no method
it does
named .getName();
pogchamp
create a class called Conclure
yes
Methods: swingBanHammer(), helpPeople() //shakiz ignore this 😛
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();
}```
ayo why do i understand this and not other code
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
yeah
the class conclure doesnt have any method called .getName();
now go back to Conclure
yuh
and put
public String getName() {
}
what happens?
You teaching shakiz on how to make a constructor?
yeah shakiz
now notice
if you turn it
public static String getName() {
}
what happens?
well
no error
you should get an error at conclure.getName()
hover over the getName with your mouse
it should give you a description of whats wrong
oh yeah forgot about shitty Java
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
oh i see
yeah
so if you capitalize the first 'C' in conclure it works
Conclure.getName() would refer to that you run the method from the class
so like this line isnt needed anymore ?
anyways shakiz lets sum up this a bit now
dont delete it
we're not done yet
so we can say
ok
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()
but String doesnt
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
hmm but doenst that mean that this should work ?
we'll come back to that later
because getprefix is static
ok
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?
yess
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?
i dont understand the question
can we call a class something else?
well if a String is a class and a type then yes ?
yup
so every class could be a type ?
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()
yeah now that i think about it
thats because int, boolean, long, double, char, byte, short, float are all primitive types
they have no class
ok i get it
yeah lets go back to your original problem
I think its enough for today
we can continue tomorrow
ok
so i have to change that from String ?
Lets Discuss Fundamental Java with Shakiz
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
fix yo class method name
:o i just did it
damn what conclure said makes sense now
i just made the CustomChat => customChat and removed "static" from the class
Conclure is good at Java. of course it makes sense 😄