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 Variable question from a beginner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variable question from a beginner" Watch "Variable question from a beginner" New topic
Author

Variable question from a beginner

Jimmy Johnston
Greenhorn

Joined: Feb 24, 2013
Posts: 2
Hello all, this is my first post and thanks for any and all replies. I am teaching myself Java from books/tutorials and videos on the internet. The following program places a target in the center of the screen.
it works properly as posted, but when I attempt to declare the variables (canvasWidth & canvasHeight) as class variables, so as to declare them only once, it places the target in the top left corner of the screen. I have commented out where I attempted to do this, but the sub-methods (?) did not recognize the variables. Also, it will not accept a println call outside of the submethods. I realize this is Java 101, but I am very new to Java and still trying to understand the basics. Once again, thanks for all replies.

Ankit Dan
Ranch Hand

Joined: Aug 31, 2012
Posts: 47

The System.out.println() on a variable will never work inside something that will never be executed as the entry point is always main so System.out.println() should either be inside main() method or some other method that ll be called by main in the program runtime

Hope this helps


Ankit
Jimmy Johnston
Greenhorn

Joined: Feb 24, 2013
Posts: 2
Thanks Ankit, that that explains the println issue. Now if I could just figure out why I declare class variables.

-JJ
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Welcome to the Ranch
What class variables? You mean instance variables, surely? You can put them anywhere outside any methods or initialisers, but conventionally they are declared first in the class.Make sure to give them private access; you might need getXXX methods to permit access outside that class. Also make sure to initialise all those fields to some sensible value in the constructor.
There must be some way to make the Canvas appear, but I don’t know it. These acm programs seem to be different from everybody else’s.
You would usually make a frame visible like this:-The link I quoted uses the show() method; you should use setVisible() instead.
This example assumes the MyGUI constructor can set up the GUI including its size and set a closing operation. For non‑GUI applications, you can use a simpler main method. Is there a main method elsewhere in one of those acm packages?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Jimmy Johnston wrote:Thanks Ankit, that that explains the println issue. Now if I could just figure out why I declare class variables.

-JJ


Hi Jimmy

Welcome. I would advise you to read the Sierra-Bates book SCJP for Java 6 first. It has all the important fundamental concepts regarding the Java language explained in simple and easy to understand language. Do the self test exercises at the end of each chapter religiously. If you try and figure out a lot of things at the same time, it is imperative that you will get confused. Build up a strong base of fundamentals first. Understand basic concepts of declarations, initializations, OOPS concepts(inheritance, encapsulation etc.. ) constructors, overloading and overriding of methods, compile-time constants, exceptions, flow control, primitives , wrapper classes, String class, File Input/Output, Formatting, Tokenizing, Threads, Serialization concepts, abstract classes and interfaces, synchronization etc. Leave the abstract window toolkit and graphics packages for later. Write small code snippets in notepad and then run it using command prompt. Do not use IDEs directly. Let me know in case you have any issues.


~ Mansukh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Good advice, except this bit
Mansukhdeep Thind wrote: . . . Write small code snippets in notepad . . .
MS NotePad is not at all good for programming. This post should tell you about something far better.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Well, closing braces properly is also a part of the learning process in my books. The post that you provided a link to was started by a guy who was a novice and was using netbeans IDE directly to write some code. He royally goofed up the proper formatting. The point I am trying to make is that one must learn the English alphabets first, then learn to make different words with these alphabets, and finally make proper sentences with these words. People directly try and write beautiful essays without being thorough with the semantics. I just want to show some light to these misguided souls. But as the old saying goes, only you can teach yourself, no one else can.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
You only need the English alphabet for keywords; you can write in any language for everything else.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7643
    
  19

Mansukhdeep Thind wrote:I would advise you to read the Sierra-Bates book SCJP for Java 6 first.

Sorry, but I can't agree with that particular piece of advice. That book is only useful if you want to take the SCJP exam...

@Jimmy: ...and at this stage it's more likely to fill your head with all sorts of stuff that will distract you from learning the language.

All the other advice is great, but I would suggest doing it with a book like Head First Java (also by the great Kathy Sierra), which is likely to be far more newbie-friendly.

There is also a page on this site that contains a lot of book recommendations, but I'm darned if I can find it at the moment (I'm obviously missing a basic keyword in my search ). Hopefully, someone else will know the page I mean.

That said, here are a few things to remember when coding:
  • You must be accurate: class is NOT the same thing as Class, and Java is case-sensitive.
  • Indent your code properly. This helps to avoid problems like missing (or excess) braces.
  • Use proper Java coding conventions, particularly when it comes to names.

  • HIH

    Winston

    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Mansukhdeep Thind
    Ranch Hand

    Joined: Jul 27, 2010
    Posts: 1157

    Well Winston, you share your first name with one of the greatest British leaders of all times and you have much more posts and knowledge to your credit than me. Hence, I would have to agree with you. I have not studied Java from the book you mentioned. So I can't comment on its approach of teaching a novice.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38353
        
      23
    Go to the top level of this website, where it says JavaRanch and look for the heading Java Books riddle the shelves at the JavaRanch Bunkhouse. That’s where you find out about books.
    Mansukhdeep Thind
    Ranch Hand

    Joined: Jul 27, 2010
    Posts: 1157

    Respect ma main man!!
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7643
        
      19

    Campbell Ritchie wrote:Go to the top level of this website, where it says JavaRanch and look for the heading Java Books riddle the shelves at the JavaRanch Bunkhouse. That’s where you find out about books.

    Cheers mate. I knew it was somewhere.

    Weirdly enough, neither a search for 'book' nor 'books' gives you that page. But presumably they assumed that people aren't as unobservant as me.

    Winston
    Mansukhdeep Thind
    Ranch Hand

    Joined: Jul 27, 2010
    Posts: 1157

    Ritchie and Winston, I need a piece of advice. Now that I am well acquainted with the basics of Java 6, I would like to take a step forward and study J2EE technologies like Servlets, JSPs etc. Could you suggest a good book for laying a solid foundation of fundamentals?
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7643
        
      19

    Mansukhdeep Thind wrote:Ritchie and Winston, I need a piece of advice. Now that I am well acquainted with the basics of Java 6, I would like to take a step forward and study J2EE technologies like Servlets, JSPs etc. Could you suggest a good book for laying a solid foundation of fundamentals?

    Yes. This one. It won't teach you anything about any of those subjects; but it will teach how (and, most importantly, why) to write good Java code. Furthermore, it's a great reference to have around.

    Winston
    Mansukhdeep Thind
    Ranch Hand

    Joined: Jul 27, 2010
    Posts: 1157

    Winston Gutkowski wrote:
    Mansukhdeep Thind wrote:Ritchie and Winston, I need a piece of advice. Now that I am well acquainted with the basics of Java 6, I would like to take a step forward and study J2EE technologies like Servlets, JSPs etc. Could you suggest a good book for laying a solid foundation of fundamentals?

    Yes. This one. It won't teach you anything about any of those subjects; but it will teach how (and, most importantly, why) to write good Java code. Furthermore, it's a great reference to have around.

    Winston


    OK. And what about learning J2EE? Will "Head First Servlets and JSPs" do the trick?
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38353
        
      23
    Winston Gutkowski wrote: . . . Weirdly enough, neither a search for 'book' nor 'books' gives you that page. But presumably they assumed that people aren't as unobservant as me.

    Winston
    It does now
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38353
        
      23
    Mansukhdeep Thind wrote: . . . OK. And what about learning J2EE? Will "Head First Servlets and JSPs" do the trick?
    No idea.

    Probably not.
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7643
        
      19

    Mansukhdeep Thind wrote:OK. And what about learning J2EE? Will "Head First Servlets and JSPs" do the trick?

    Quite possibly. I'm a big fan of O'Reilly when it comes to that specialized stuff.

    But my advice to you is to learn how to write good basic Java before you embark on new frontiers. To that end, a good book on design patterns might be something to think about, of which the granddaddy is this one. It will give you a good grounding in the things that J2EE actually implements.

    Slow and easy does it. You have plenty of time. :

    Winston
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7643
        
      19

    Campbell Ritchie wrote:It does now

    Oh. Was I just being thick or have you done some magic? I'm quite prepared to believe it was the former.

    Winston
    Mansukhdeep Thind
    Ranch Hand

    Joined: Jul 27, 2010
    Posts: 1157

    OK Winston. Then I am ordering a hard copy of the book "Effective Java" by Joshua Bloch as suggested by you. It should be worth the investment. Thanks for your advice.
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7643
        
      19

    Mansukhdeep Thind wrote:OK Winston. Then I am ordering a hard copy of the book "Effective Java" by Joshua Bloch as suggested by you. It should be worth the investment. Thanks for your advice.

    And I'm sure you'll also also be glad to know that, when you read it, you're being advised by one of the people who actually programmed many of the classes you use. Who better to tell you some of the things they did wrong? (and he does)

    Winston
    Mansukhdeep Thind
    Ranch Hand

    Joined: Jul 27, 2010
    Posts: 1157

    RESPECT to ma main man Joshua BLOCH!!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Variable question from a beginner