• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

constructor doubt

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone help me understand why the compiler gave me error with following code on the line this(4)

public Test6{
public Test6(){
this(4);
}
public Test6(byte var){
System.out.println(var);
}

public static void main(String[] args){
Test6 t6 = new Test6();
}
}

Thanks
Mary
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if this is all but you're definitely missing the class keyword!
Next time, please use the CODE button and paste your code between the inserted tags.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is you complete code


You got error on line 3 because you are trying to invoke a constructor that takes an int, 4 is an int, and you only have a constructor that takes a byte so you need to cast 4 to byte, replace line 3 with:
 
lowercase baba
Posts: 13073
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mary, and welcome to the Ranch.

As a general note, when you post a question about a compiler, error, it is of GREAT help to people reading your post if you post the actual compiler error. cut'n'past the whole thing - they're not that big, we can afford the storage space.

Seriously, it does help a lot - it saves other from copying your code into an editor, saving it, and compiling it themselves.
 
Mary John
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI all

thanks for the responses,

But I still am not convinced, there is another rule that I read somewhere that says you dont need to cast an integer in the range -128 to 127. any integer in this range is automatically cast to a byte.
so then why does it require casting here. ?

thanks,
Mary
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


an integer in the range -128 to 127. any integer in this range is automatically cast to a byte.


Thats correct but only when make variable declaration.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You are just passing value i.e 4 so by default it consider as int so it can't do down casting so byte and short are not valid at this point.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whenever you pass or assign a value to byte variable other than declaration statement (in the short range -128 to +127 ) then automatic conversion is done by compiler.

but if you are assigning it on other than declaration, you should explicitely convert it. Also, while doing any operations on the variable.
otherwise you'll get loss of pricision error.
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic