Saltar al reproductorSaltar al contenido principal
  • hace 5 semanas
En este video se explica que es static.

Categoría

📚
Aprendizaje
Transcripción
00:00Hello, welcome to a new video about the stati modifier in Java.
00:08Well, in Java the stati modifier is used to define members of a class, attributes or methods,
00:17that belong to the class itself rather than the object instance.
00:22So what does this mean? That when you create an instance of a class,
00:33that instance, the static attribute or method will belong to the class, not the instance.
00:44So, let's look at it with some examples I have back.
00:49Now, we have a class called car, which has public access modifier with two attributes,
01:03two attributes called brand and another model,
01:10which we have an integer type stati variable called car counter.
01:20And we have the attribute parameterized with two parameters, brand and model,
01:29which is initialized.
01:30And here we see something that is the car counter.
01:38What does this do?
01:39That every time a car object is created, a car instance,
01:46What this will do is increase the car counter variable by one.
01:54What's happening?
01:55That every time a car object is created, what it will do is that this variable,
02:02As it is of all classes, it will show the number of cars that are being created.
02:11So here we're going to do a sample, here we're going to create, here we're going to instance,
02:24let's create a car object, then, car here ready.
02:31So, once that object is created, the counter will increase by one,
02:41This is going to increase by one, because as we can see, we have here the instance where that object was created.
02:52And we can see that every time that car class is instantiated, the car counter is incremented.
03:07So what's going on here?
03:12This method, we have a static method that does not return anything.
03:16What does a static method mean?
03:18A static method, also, is one that is of the class, is of the class, but is not instantiated.
03:30So you don't need to create one, create an instance where it is,
03:37an instance of a class where that method is located so that it can be accessed.
03:42But we can access, access that method directly.
03:48There you can access it without having to create an instance.
03:53And here mark one, because we created a single object.
04:00But if we create multiple objects, here I show you.
04:03The result would be 3.
04:15As we can see, correct.
04:17For that,
04:21Here we have,
04:22Here we have another one,
04:26another example of static methods.
04:28Here we have,
04:30Here we have this class called mathematics,
04:34which contains the operations,
04:37four basic operations,
04:39addition, subtraction, multiplication and division.
04:42What this static method does,
04:45which returns an integer.
04:48And, by parameter,
04:51It needs two parameters,
04:53two integers.
04:54And this is going to return depending on what the method is.
05:00If it is the sum,
05:01returns the sum of these two parameters that we send to it.
05:05So,
05:06as they are static,
05:08it is necessary to create one,
05:11create an object of that class to be able to access its methods.
05:15But,
05:17that already being within the same package,
05:19we can access it
05:21directly.
05:23So,
05:24we make a,
05:27here we will try
05:31how the sum works.
05:36And we access it directly.
05:37And when executing it,
05:39we see that the sum is 5
05:43and everything works correctly.
05:46So
05:51and
05:51and
05:52and
Sé la primera persona en añadir un comentario
Añade tu comentario

Recomendada