+ 4

Enum,private,finale

hey guys i am new in programming and I have a understanding question .. can anyone please explain me in simple words what enum,private,finale are? i read much in the internet but i dont understand it and it would be nice if you can give me an example another question is when do programer know when to use long, byte .. thank you all for your time-investment i really apprechiate.

18th Sep 2017, 10:06 PM
Elsayed Salama
Elsayed Salama - avatar
4 odpowiedzi
+ 5
By finale do you mean final? And I would assume this is Java, not C#? (C# doesn't have final). (BTW when I use the term 'member' I'm referring to something inside a class. This can be a method or instance variable) Private: A member that is private means that it is only accessible by the class it is inside. You cannot access it from outside the class! If this is confusing, please look into what classes are, and what it looks like to have more than one class. Enums: Enums are a little bit more advanced. Enums have Constants that have their own type and value. It's just a very handy way to deal with a bunch of constant variables. According to Oracle: "You should use enum types any time you need to represent a fixed set of constants." What's cool is that an enum is really a special kind of class. It can actually have it's own methods and variables! Final: Now, final actually does a bunch of different things. When used on a variable, The variable MUST be assigned a value right away, or in a constructor and it CANNOT be re-assigned. This is the keyword we use to create a Constant in Java (without Enums). This is equivalent to the 'readonly' keyword in C#. Example/ final int PI = 3.14; // Cannot be re-assigned PI = 5; // ERROR However, when the final keyword is used on a method it will not allow the method to be Overrided. This is equivalent to 'sealed override' in C# on a method When final is used on a Class, the class can NOT be subclassed. Preventing the class from being a super class. This is equivalent to 'sealed' in C# on a class.
18th Sep 2017, 10:27 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
thank you very much now I understand it !!
18th Sep 2017, 10:32 PM
Elsayed Salama
Elsayed Salama - avatar
+ 3
I'm going to start in reverse order as it is easier to explain : FINAL is used when you want a value to stay constant no matter where it is accessed from. It cannot be changed by any external /internal programs or methods. Example : if you are creating a physics program and the speed of light is used throughout your program, you will want to make that speed of light value a FINAL type since it is a constant value in physics (public final double speedOfLigjt=3x10^8)... Private variables can only be accessed by the class it is created in. So if you have many other classes, any variable that is made private will not be accessible to all other classes except the class it was created(declared) in. That's all it does, no need for example I assume? Enum is special from the other 2. They are similar to lists or Hashs. Inside of a Enum variable, you can have many other variables. Example : say you want to make a program that makes the user change the colour of a ball (either red, green or blue), you would get the input from the user and then check if the user's Input matches the ball colour. It would be easier to use enums in this case (enum Ballcolour{ RED, GREEN, BLUE } //now you just get the user input eg:if the user enters "Red" for red you will create a enum variable called "ball" and set it to red// Ballcolour ball; //created a enum variable If (user input.equalsIgnoreCase("red")) ball=Ballcolour. RED;
18th Sep 2017, 10:48 PM
Adhil
Adhil - avatar
+ 3
sir thank you very much !!
19th Sep 2017, 2:53 AM
Elsayed Salama
Elsayed Salama - avatar