• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem returning a value  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

dont know why im still battling with this...

i just dont understand the point of returning a value,or more how/when that value gets used
was mucking around with a small program and wondered if you could tell me in what situation would i return a value to the calling method
and where it goes...now i know you cant have void in you method if you want it to return,i just dont have any return till i see what you guys have to say

my code

first class ComputerShop



second class Customer





 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Perhaps, it would be easier if you had a requirement that needed a method to return something? For example, instead of a customerDetails() method (that prints the age), how about an calculateAge() method that returns the age?  Can you try to implement that?

Henry
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks will give it a go in the morning
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have completely restructured this code
but still battling,trying to structure like oracle tutorials

Customer class



second class



i added a constructor,
but i just cant seem to get hold of the right variables in my main method.

i dont know,the theory is explainable,its the practical application im battling with

thanks for reading

 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is perfectly legal code that doesn't do anything useful.  The reason has to do with variable scope, or where a variable is visible and can be accessed.

A variable is visible only in the block of code it is declared in.  Broadly, a block is between to braces ({}).  You declare, for instance, String name and initialize it to customerName.  But since it is declared inside the constructor, it can only be seen inside the constructor.

The trick is to declare the variable outside of any constructor, method, or initializer block.  Instead, declare it only inside of the class block, usually at the very top:

Now name can be seen anywhere inside the Customer class.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what is the point of a constructor really...thanks for your help
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Does this compile ?

I am pretty sure the return should return a value.
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:so what is the point of a constructor really...thanks for your help

As the name suggests, a constructor is used to 'construct' an object. Its an initializing block of code where you would typically initialize the variables in your class at the time its object is made.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More precisely, the constructor shou‍ld get your object into a consistent state, i.e. establish all the class invariants. Informally it means to initialise all the fields to “sensible” values. The advantage a constructor has over some means of initialisation is that you can have different values for each field. Only rely on setXXX methods if you are using an implementation of the Bean Pattern and you are in a trusted environment. If you go through Effective Java™ by Joshua Bloch, you will find out the advantages of factory methods.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the cow
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!