Variavel de Instancia e Variavel de Classe - Linguagem Java

Tecnologia, Direito e Inovação
29 Aug 202203:07

Summary

TLDRIn this tutorial, the presenter explains the difference between instance variables and class variables in object-oriented programming. Instance variables are specific to each object created, with a unique value for every instance. In contrast, class variables, marked as static, are shared among all instances of a class, maintaining a common value across all objects. The example uses a 'Student' class where instance variables store individual details like the student's name and ID, while the class variable tracks the number of students. The video highlights how these variables function and differ in how they are associated with instances or the class itself.

Takeaways

  • 😀 Variables of instance are unique to each object created, meaning each object gets its own value.
  • 😀 In the example, the 'Aluno' class uses instance variables for 'nome' (name) and 'matrícula' (registration), each being set individually for each object.
  • 😀 A class variable is shared by all objects created from the same class, meaning it holds a common value across instances.
  • 😀 In the provided example, the 'quantidade' (quantity) variable is a class variable, initialized to zero and incremented as new 'Aluno' objects are created.
  • 😀 Class variables are declared using the 'static' keyword in the class, differentiating them from instance variables.
  • 😀 When a new 'Aluno' object is created, the class variable 'quantidade' is incremented, and the new value is shared by all objects.
  • 😀 If you print the class variable from different instances, it will always display the same value, as it is shared by all objects.
  • 😀 For example, after creating objects 'A1', 'A2', and 'A3', the 'quantidade' variable will be 3, showing that the class variable is shared across all instances.
  • 😀 Instance variables are associated with individual objects, so they can vary between objects, while class variables are associated with the entire class and remain the same across objects.
  • 😀 Understanding the distinction between instance and class variables is crucial for better object-oriented programming (OOP) in languages like Java and Python.
  • 😀 The script emphasizes that class variables are suitable when you need a value that is consistent across all instances of a class, whereas instance variables hold values specific to each object.

Q & A

  • What is the difference between a class variable and an instance variable in the context of object-oriented programming?

    -An instance variable is associated with an individual object and has a unique value for each instance of a class. A class variable, on the other hand, is shared by all instances of a class and is typically declared as 'static'.

  • How does an instance variable behave when new objects are created?

    -Each time a new object is created, the instance variable receives a unique value for that specific object.

  • Can you provide an example of an instance variable in the script?

    -In the script, the 'nome' and 'matrícula' are instance variables because they hold unique values for each object created (e.g., 'João' for A1 and 'José' for A2).

  • What is the role of a class variable in object-oriented programming?

    -A class variable is shared by all instances of a class. It is common to all objects and its value is incremented or changed across all instances when modified.

  • How is the class variable 'quant' used in the script?

    -'quant' is a class variable declared as static. It starts at 0 and is incremented each time a new 'aluno' (student) object is created. Its value is shared by all instances of the class.

  • What happens when you print the class variable 'quant' from different objects?

    -Since 'quant' is a class variable, it will print the same value for all objects, regardless of which object is used to print it. For example, printing 'A1.quant' or 'A2.quant' both show '3'.

  • What does it mean when the class variable is shared by all objects?

    -It means that the value of the class variable is not specific to any single object but is common across all instances of that class. Any change made to this variable will be reflected in all objects.

  • How is the class variable 'quant' modified in the script?

    -'quant' is incremented by 1 each time a new 'aluno' object is created. This increment happens regardless of which object is being created.

  • What would be the output if we print the class variable 'quant' from different objects after creating three objects?

    -The output will be '3', as 'quant' is a class variable and shared by all objects. After creating three objects, the value of 'quant' is incremented to 3, and all objects share this value.

  • Why is 'quant' considered a class variable and not an instance variable?

    -'quant' is considered a class variable because it is declared as static and its value is shared by all instances of the class. It is not tied to any specific object but to the class itself.

Outlines

plate

هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.

قم بالترقية الآن

Mindmap

plate

هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.

قم بالترقية الآن

Keywords

plate

هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.

قم بالترقية الآن

Highlights

plate

هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.

قم بالترقية الآن

Transcripts

plate

هذا القسم متوفر فقط للمشتركين. يرجى الترقية للوصول إلى هذه الميزة.

قم بالترقية الآن
Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
PythonObject-OrientedClass VariablesInstance VariablesProgramming BasicsCoding TutorialTech EducationLearning PythonProgramming ConceptsPython Classes
هل تحتاج إلى تلخيص باللغة الإنجليزية؟