![]() |
Why use return statement (newbie)
Hi folks!
I am complete newbie to OOP, and generally quite new to any programming so forgive me my probably silly question. Why use return statement like this: Code:
class NumCode:
class Num {Code:
... |
I'm not sure why you think this will work:
PHP Code:
Public properties should be avoided as you cannot run any verification or validation if it is left public. It should be private or protected, and accesor and mutator functions written for them. If the main were in a class with a private property, it can still access that property, but it cannot access it if its not a part of the same class. |
Quote:
Code:
class Num { |
for your properties (I think called 'fields' in Java) you generally just do a return of the variable...
Code:
public int getNum()Code:
private int computeNewNum(int varibleNumber)Code:
private int computeNewNum(int variableNumber)making a field private means that only that object can access it and change it via method. Code:
public class DemoCode:
public void seti(int variableNumber) |
Quote:
Code:
public class Main {Quote:
|
If I understand your question 'why use return in some methods?'.
You only use 'return' when your method is declared with any of java primitive types. Remember the format for declaring method: accessModifier type methodName(parameters) { statements; } 'void' is a type that doesnt return anything,so it doesnt use return. E.g: Consider me(int) sending you(statements) to the market, when you return, you must give me a feedback(return statement). |
If I understand your question 'why use return in some methods?'.
You only use 'return' when your method is declared with any of java primitive types. Remember the format for declaring method: accessModifier type methodName(parameters) { statements; } 'void' is a type that doesnt return anything,so it doesnt use return. E.g: Consider me(int) sending you(statements) to the market, when you return, you must give me a feedback(return statement). |
Quote:
|
The reason why is that you do not want public properties at all.
PHP Code:
This is why you should be returning even properties for a class instead of making them public. The only thing that should be specified as public should also be affixed with the final property on it.In other words, dynamic variables should never be accessible using aninstance.property. They should always be accessible using aninstance.setProperty(avalue); and aninstance.getProperty(avalue);. Languages like C# use set overloads to chain the get and set directly to the dereferencing of the property itself, so by calling aninstance.property = avalue chains the call to the set associated with 'property'. Java does not have this ability. Its been awhile since I've done C#, so excuse me if I biff it up a bit.In C# you do as so: PHP Code:
PHP Code:
|
Between Fou-Lu's and my own examples/explanations, as well as greeninho's clarification on the return types (ie void, int, double, etc) I don't think there is anything more we can do to make it "more" clear... I think what you should do (because at this point I am guessing you are understanding what we are saying, but the "ah-ha!" moment hasn't hit yet) is just take it on faith that good coding practice is done this way. And albeit a piss-poor explanation- for now all you really need (since you are a "newbie") is...
Quote:
|
Ok guys.
Thanks for explanations. So as far as I understand: along with good programming practice I should put private/protected for variables as well as for methods which process these variables, and write separate public method which returns result of these methods (their process - calculations, changing, etc.)? In other words - methods in class should be private/protected so no one can interfere from outside. For these methods should by public methods which returns their result? |
Quote:
|
Quote:
Code:
class Num {Code:
public class Bicycle{Code:
... |
You can only invoke it this way since you are writing the main method within the class itself. Create a new class with a main method and invoke the same and you will get an compilation error. I mentioned that main methods within a class are considered within a valid scope, and as such can access private properties.
This code of course wouldn't compile. MountainBicycle is not provided with a return type, nor is it a constructor for the class Bicycle. Constructors only need to be provided with the public scope if you wish to allow construction using new MyClassType(). If you do not want to allow instance creation with the new keyword, then a private/protected scope is added to prevent that.Remember the basic scopes are: private - only this class can access, protected - only this class and its children can access, public - anything can access. The final modifier is one without an explicit access modifier, and it is best thought of as package. This page describes these modifiers: http://docs.oracle.com/javase/tutori...sscontrol.html Note that the last line also indicates that you should not use public, even though most examples on the site do. |
Thanks again for explanation. I have no idea where I got this notion from - that private fields become public in public methods, so they would be accessible. That's the whole misunderstanding. Thanks again. Going back to learning.
|
| All times are GMT +1. The time now is 03:20 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.