• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple variable and object question.  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys. So this isn't really for anything, I'm just programming random useless programs to try and get a better grasp of the various areas in Java. I've come to a (probably silly) problem though.

This code just won't compile:



I'm super new to Java, so I'm probably just doing something wrong, or thinking of it like a different language.

As far as I can see, I've created an object of cat, and already defined the int cry variable. But when I try and compile the code, I get this:



I'm trying to get better at reading the error screens, so I can be a bit more self-sufficient, so if it's possible to relate what I'm doing wrong to the error screen in a bit easier to understand language, I would greatly appreciate it!

Thanks in advance!
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The relevant part of the reported error is:
aaa.java:5: error: cannot find symbol
a.cry = 5;

I then looked at where cry was defined. It's a local variable inside the main method. You are accessing it as if it were an instance variable in the cat class. Does this give you enough of a hint to fix it?

And it's good to program to learn! It doesn't have to be "for something."

ps - Java convention has classes beginning with capital letters. You might want to rename cat to Cat.
 
Kyle Fraser
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I tried a different approach, by having two separate classes, Cat and catTest. Defining cry inside Cat, and then creating a new cat object inside catTest, as well as moving main over to catTest. I've done this before, and I don't know what I'm doing wrong. Maybe I'm just brainfarting.









The error when I try and compile catTest:




They are in the same folder. In their own folder for that matter.
 
Kyle Fraser
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I also fixed the capitalization, to the same error.
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are new to the Java language and we have all been there at some point in time, I feel your pain...

So I am going to provide you with a quick run down:

The first thing you should do is access a book that will introduce you to the language. This will save you some of the headache from guessing.

Now a class is a description of a type. It describes the state and behaviour of that type by using fields and methods. From what you have provided you are on the right track but It should be told that Java is a case sensitive language so Cat and cat is not the same. The variable cry is part of the Cat class so you cannot access it as a standalone variable outside of the class so you should be observant that System.out.println(cry); is totally wrong since that variable is no where in scope in the class from which you are making the call.
 
Kyle Fraser
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have figured it out! This is what I love about programming. A problem gives you such a hard time, you start to feel bad about yourself.

But maaaan, when you figure it out, it feels fantastic. Like one of the best feelings to me.

Thanks a ton to you guys, you are prime reasons why I am always on this forum, even if I don't post much. And also thanks to the mod who pointed out the Code tag to me!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!