¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita0
Is this assigning bark with "woof"?
public class Dog{ static void bark(){System.out.println("woof");} } thanks
5 Respuestas
+ 6
This is a void function 'bark' which prints "woof" to the console. It is different from assigning a value to a variable. A function contains a block of code which is executed when the function is called.
+ 3
Within the main itself you will have to instantiate dog object and make a call to bark function.
Dog d = new Dog();
d.bark();
+ 3
This would work too. I am not creating a separate object here but only calling the function bark from main
public class Program
{
public static void main(String[] args) {
bark();
}
static void bark(){System.out.println("woof");
}
}
0
how come i can call it from main without it being assigned using Dog.bark();?
0
@ apoorva i can do this without having to instantiate dog object i can just do dog.bark(); and it works??
would doing it your way allow me to change bark(); to somthing else if i was going to create another animal?