File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Just started today, please help with basic knowledge Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Just started today, please help with basic knowledge" Watch "Just started today, please help with basic knowledge" New topic
Author

Just started today, please help with basic knowledge

Ryan Wamsat
Greenhorn

Joined: Aug 06, 2012
Posts: 13
I decided to start to learn to program, and I've already gone through a few tutorials. But, at this point the knowledge hasn't sunk in. I was hoping to get some help in base knowledge. I would appreciate an explanation of what is wrong... not a code fix.

I've given myself the task of writing a program with the following goals:
1. How many days have you been alive?
2. How many hours have you been alive?
3. How many minutes have you been alive?
4. How many seconds have you been alive?
5. What milestones have you passed?
a. 16 - Driver's License
b. 18 - Adult
c. 21 - Alcohol
d. 25 - Cheaper insurance
e. 35 - Can be president.
f. 55 - Senior citizen
g. 65 - Retirement
h. 100 - Centennial

I stole (borrowed?) some code from a website (I would give proper credit to the author, but I lost the website) and have been trying to manipulate it to gain some understanding of how things work in Java.

This is where I'm at:
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi Ryan,

Welcome to JavaRanch

The error is you cannot have assignment statements outside a block (say method block). If you need to assign values to your variables in your class body, you have to specify them during declaration itself.

So below is a valid construct.


If you are new to Java, please start with the Oracle Java tutorials. Also use the JavaRanch Bunkhouse search to find beginner's books.
Naishadh Parmar
Ranch Hand

Joined: Jun 02, 2011
Posts: 95

If you really want to assign values to the 3 variables outside then you can do something like this:

Sorry John I realized later that me and you are on the same lines!!!
Ryan Wamsat
Greenhorn

Joined: Aug 06, 2012
Posts: 13
Thank you both for your fast reply. I can see that this is going to be a helpful forum.
So, I guess it is technically accurate to assign values to the variables at the declaration. Would that be best practice? Where/when should I be assigning these values?
Naishadh Parmar
Ranch Hand

Joined: Jun 02, 2011
Posts: 95

Yeah Ryan this is a really helpful forum. People here are very experienced and provide you with an accurate answer(I'm not praising myself!!!)

Yes, of course there is no problem in this practice. But, you can also do this-- leave the declaration where it is and assigning variables at the start of the main method
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Initial values can be assigned when variables are declared, in constructors, or prior to their first use. It depends on your application logic. Instance variables like your month, day, and year variables are assigned default values. For object references, the default is null. For int, the default is 0. Therefore, explicitly assigning 0 to the instance variables as suggested in previous replies is actually redundant.


Junilu - [How to Ask Questions] [How to Answer Questions]
Naishadh Parmar
Ranch Hand

Joined: Jun 02, 2011
Posts: 95


Yeah Junilu you are right. That thought didn't come to my mind.
 
Consider Paul's rocket mass heater.
 
subject: Just started today, please help with basic knowledge