Solved What the "new " code for?

Discussion in 'Spigot Plugin Development' started by Bruce0203, Oct 14, 2019.

  1. Code (Text):
    ItemStack death = new ItemStack(Material.LAVA_BUCKET);
    .
    .
    .
    getCommand("gui").setExecutor(new GUI_Command(this));
    according to this code, there are "new " prefix.
    What them do for?
     
  2. It creates a ***new*** instance of any object, you should try to learn java before trying to code any plugin
     
    • Agree Agree x 1
  3. Also naming conventions please
     
  4. I agree but an instance is the same as an object. You probably wanted to say that it creates an instance of a class.
     
    • Agree Agree x 1
  5. So explaining the idea of new isn't too simple at first so I would like to show you with an example.
    So lets say you want to have people on your chat room, but you want each person to have their own name
    and down the road their own ranks, ages and other details. well this is where an object/class comes in.

    It allows you to easily store bulk information and run functions with them without affecting other objects.
    This whole concept is called "OOP" object oriented programming.
    Code (Text):

    public class ExampleForBruce0203
    {
        //This is the function that java will always call when ran
        public static void main(String[] args)
        {
           //New creates a new person
            Person bob = new Person("Bob");
            Person john = new Person("john");

            john.speak("Hey");
            bob.speak("Good evening");
        }


    }

    //Normally this class will be in another file called "Person.java"
    //This defines the structure of the person Object
    class Person
    {
        private String name;

        public Person(String name)
        {
            this.name = name;
        }

        public String getName()
        {
            return this.name;
        }

        public void speak(String message)
        {
            System.out.println(this.getName() + " > " + message);
        }
    }
    Output
    Code (Text):
    john > Hey
    Bob > Good evening
     
    • Friendly Friendly x 1
  6. This is not a Java school, thank you.