• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Classes and Constructors

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I'm pretty new to Java...well so new in fact that I'm reading through Java Head First 2nd Edition.

I'm a little confused when it comes to Constructors. I can see what they are and what they do, but I don't understand why you would use them instead of just adding the variable values and/or methods to the Class itself?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

A constructor is a special block of code in a class to initialize new instances of that class (new objects - an instance = an object of a class).

You can indeed initialize member variables at the point where you declare them, in the class itself, or in a constructor, and which of those to you choose doesn't really matter a lot.

However, sometimes you need to do more than just set the values of member variables. You can't put arbitrary code statements inside the class itself - statements must be inside a constructor or a method. So, in such a case, you'll need to add a constructor to put those statements in.

Another use is when you want to be able to pass values for the member variables when you create an object. You can do that by creating a constructor with arguments. See Passing Information to a Method or a Constructor for more details.

Also, sometimes you want to control who is allowed to create new objects of your class. In that case you'll want to make the constructor protected or even private, for example. For more on this subject, see Controlling Access to Members of a Class.
 
Neil Wilkinson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper, I'll try and digest that.

I think I understand. If I want to create an instance of a class and throw something else in there before the class copy does it's thing, use a constructor.
Being as constructors are run before the class instance itself, I'm guessing that this is where the access control comes into play.

I'm a bit confused by the statements though, as I though these could also be added to a method within the class itself. do I really need to use a constructor to add statements?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neil Wilkinson wrote:If I want to create an instance of a class and throw something else in there before the class copy does it's thing, use a constructor.


I don't know what you mean with the "class copy". A class is like a blueprint for creating objects - the class is the "plan" that lays out what objects of that class look like.

For example, in the class you declare member variables and methods. Each object that you create from that class will have the member variables, and you can call the methods declared in the class on those objects.

Neil Wilkinson wrote:I'm a bit confused by the statements though, as I though these could also be added to a method within the class itself. do I really need to use a constructor to add statements?


I didn't say you needed a constructor to add statements to methods. You cannot add arbitrary statements in a class itself, outside a constructor or method.

Let's look at an example.

This class shows what a House object looks like. It has one member variable, address. It has a constructor that allows you to pass the address when you create an object using class House. It also has a method to print the address. You can use this to create House objects:

Note how we can pass the address when we create the object. That address is passed to the constructor, which saves it in the address member variable of the object you're creating. You need a constructor to be able to pass the address.

You cannot put arbitrary statements at class level. For example, this would result in a compiler error:

 
Neil Wilkinson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if I'm confusing this unnecessarily, but can't I do the same thing by creating a method in the Class and just calling that first? Or is it a case of, this information is needed before the rest of the Class is run?

I'm very black and white with these things, so sorry for all the ongoing questions. I just need to have it logged in my brain.

BTW by copy of Class, I meant instance of. I was lead to believe that when a Class instance is initiated, all I'm really doing is copying a template (blue print) of that Class to use elsewhere. A bit like using a Word template and saving it as something else.

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neil Wilkinson wrote:Sorry if I'm confusing this unnecessarily, but can't I do the same thing by creating a method in the Class and just calling that first?


You could, the same thing is possible in different ways. But it's shorter and more clear with a constructor, and if the member variable is final the way with a method will not work, because in that case the member variable must be initialized when the object is created (you can't set the value of the variable afterwards by calling a method).
 
Neil Wilkinson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper. I have a much better understanding now.

I appreciate your time.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic