0

i'm doing something wrong here... Reference type Java

hi, i just started learning java and was playing around with different data types, i got the hang of primitive data types fairly well, however when i tried to make a reference data type my software gave out an error. public class HelloWorld { public static void main(String[] args) { // TODO Auto-generated method stub Cat c = new Cat(); } } Multiple markers at this line - Cat cannot be resolved to a type. i'm not sure what i'm doing wrong, it's probably quite obvious but i'm completely new and can't see the mistake.

9th Dec 2019, 12:57 AM
Goose
4 Réponses
+ 3
Goose , Not always. For example even an array is object in Java. int [] arr=new int[5]; here `arr` is reference to integer array. but as compiler is aware of what `int` means you'll not face any errors. One more, you must have seen Scanner. Scanner sc=new Scanner(System.in); here you don't need to give any details of what Scanner is. it's already implemented. Once you import Scanner class ,you are done. Compiler understands what it means. but in case of `Cat` you need to tell how a Cat should behave(its methods) and what properties (data members) it should have. It's your own, user defined type. a simple definion of cat can be like : class Cat{ String name; void sayHi(){ System.out.println("Meow! I'm "+name); } }
9th Dec 2019, 2:51 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
0
Have you created class `Cat` ? if no then how compiler will know there exists something called `Cat`?
9th Dec 2019, 1:05 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
0
Are you supposed to do that for reference types? I'll fiddle with it.
9th Dec 2019, 2:38 AM
Goose