Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Constructors  RSS feed

 
H Adusumilly
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am a beginner trying to learn java. I am trying to write code for parameterized constructors. The code is below. Please help me understand if I am writing the code correct or not. If not correct me.

I have an employee class with the following code.

This doesn't show any compilation error. I have another class with the name DefaultConstructor. It has the following code in it.




The line 9 in the above code throws a compilation error.
I want the cal and sum values automatically generated with the logic I gave in the Employee class. So what do I do for this code to work?


Thanks in advance!
 
Carey Brown
Bartender
Posts: 2985
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your "double cal" should be replaced by a double variable. Perhaps "cal" is your double variable, then your call would be

When you invoke a constructor you don't declare the type again as the compiler can infer the type from the variable or constant passed in.
 
H Adusumilly
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carey,

Thank you for your response!!!

I have tried doing that earlier but it throws the following Exception:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
cal cannot be resolved to a variable
sum cannot be resolved to a variable


That's the reason I tried giving their datatypes again.

Regards
 
Carey Brown
Bartender
Posts: 2985
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H Ad wrote:Hi Carey,

Thank you for your response!!!

I have tried doing that earlier but it throws the following Exception:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
cal cannot be resolved to a variable
sum cannot be resolved to a variable


That's the reason I tried giving their datatypes again.

Regards

Exception message seems self explanatory. You don't have a variable "cal". You could create one with the desired value or you could replace it with a constant (e.g. 4.5). All your other invocations of the constructors are using constants except this one.
 
H Adusumilly
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey,

I have the variable cal in the Employee class if you see otherwise even the Employee class would throw a compilation error.




I know that the other variables I have in my code are constants. Unlike them I want these two values to be calculated based on the logic I wrote. But I do not know how to make them work. I am bothered, is it even the right thing to do? I have no clue.


Regards
 
Carey Brown
Bartender
Posts: 2985
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this constructor you are passing in a value that will be referred to as "num" within the context of this constructor. It could care less what the caller refers to it as. Also your DefaultConstructor class knows nothing about the fields and variables defined in you Employee class.

Here you call that constructor with constants. No "num" or "name" at all.

You are trying to do this differently with the new constructor and that's what is getting you into trouble.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, each constructor should assign a default value to all fields that are not passed in as parameters. For example, your constructor

creates a new Employee with no name or employee number specified. It is often suggested that there be one constructor that has all relevant fields as its arguments (it might even be private) that all other constructors invoke as their only statement; That way if you make a change, all constructors get the change. For example,


As a side note, you should follow convention and make all of your variables begin with a lower case letter.
 
H Adusumilly
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey,

So should I necessarily give constants in there? Can the logic not work ?

Is there any other way to call the logic rather than giving constants.

Regards
 
Carey Brown
Bartender
Posts: 2985
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H Ad wrote:Carey,

So should I necessarily give constants in there? Can the logic not work ?

Is there any other way to call the logic rather than giving constants.

Regards



Ok, let's step back. This constructor is flawed. You are passing in "cal" yet you stomp on it with "cal=((no*5)/10)". It would seem that you don't want to pass in "cal" at all and just compute "cal" locally. Ditto for "sum".

So you might end up with something like

Double check your formula. Looks a little suspect to me, but I could be wrong.

Also, please use descriptive variable and field names. "i", "j", and "k" don't tell us what they represent.
 
Carey Brown
Bartender
Posts: 2985
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:

Always begin variable and field names with a lower case letter.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am going to disagree with everybody; I have three things I believe about construcotrs.
  • 1: You must write a constructor for every class you write. Otherwise the compiler will add a default constructor which might not be what you want.
  • 2: Every constructor must initialise every field in the class (directly or indirectly) to a consistent value. As a rule of thumb, regard anything allowing nulls as a mistake until proven otherwise.
  • 3: You should provide as few constructors as possible.
  • If you provide two constructors, you are providing code two ways to instantiate your object. The mre ways you have to instantiate things, the more scope there is for something going wrong. Look at my Kettle class; all the field names should be obvious except minFill which means the least amount of water to cover the element:-That is a sole constructor; there is no chance of creating a kettle without providing those three pieces of data. You can enhance that constructor to refuse wrong arguments, e.g. negative capacity. Two of the fields are initialised to default values; every new kettle contains no water and is at room temperature, although those values will change as soon as you try to take it home (especially in Winter ‍). Note the idiom with this.make. That means that the field called make is on the left of the = operator and the local variable or parameter of the same name is on the right. I believe that is what a constructor should look like. And never use the keyword this in a constructor in any form other than that shown above. Only ever call methods from the constructor which are marked private or final.
    CB is right to complain about the field names i j k. Especially if they are doubles; i j k should be reserved for ints.

    You will doubtless have been taught that constructors are there to permit creation of objects. What you probably have not be told is that constructors are there to restrict creation of objects. You write one constructor, which means, “You can create objects my way, or not at all.”
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!