This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Is Java too difficult for me? Trying to learn with Head first Javabook Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is Java too difficult for me? Trying to learn with Head first Javabook" Watch "Is Java too difficult for me? Trying to learn with Head first Javabook" New topic
Author

Is Java too difficult for me? Trying to learn with Head first Javabook

Daniel Lewis
Greenhorn

Joined: Dec 20, 2011
Posts: 10
Hi folks! I really thought, that the book Head first Java (from Kathy Sierra and Bert Bates) would give me an good start into the world of java. I am now on page 38. This section of the book tells me (ok is trying to tell me...) java and its object orientated roots. I read this section 5 times and over and over again, but I did not get the point. At the moment I am very frustrated, cause I always get stuck @some point (also in python for example), which then prevents doing the really interesting things in programming.

As I did not understand this part of the book, I don't think it is useful to just "read on". If you ask me when to use classes, which instancevariables to set, or how to create an reference to an object I cannot give an correct answer. To say it in other words: "I cannot see the sense in it". I am missing some exercises to learn by doing... As I mentioned python before, I think the "Interpreter" in python makes it easier to figure out how the "object" thing is running...

The real annoying thing is, that I got this "am I too thumb for programming" feeling inside of my... (again)

Have you got any suggestions for me?

Greetings
Daniel
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

You definitely need to gain an understanding of the basic concepts of OOP: classes, objects, methods, instance variables, etc. Everything in Java is based on those concepts.

Essentially, you are dealing with "things" (objects) and the things they can do (methods) and the information they hold (instance variables).

The 'sense in it' is that's the way Java works.

If you can formulate a specific problem into a question, maybe someone can answer it and give you a push in the right direction.


Keith Rainey
OCPJP6
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3504
    
  14
Daniel Lewis wrote:I read this section 5 times and over and over again, but I did not get the point.

At this point I'd look for another source of information. Another book explaining the same thing but in a different way may make more sense to you. Try the BunkHouse for some suggestions.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
. . . and welcome to the Ranch
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

I'd recommend that you type out the examples in the book and compile them. Once you get the output that you're supposed to get, start playing with the code; take stuff out, make changes, etc - that way, you'll be able to see what each line of code is doing.


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Daniel Lewis
Greenhorn

Joined: Dec 20, 2011
Posts: 10
Hi again! thanks for the replies. I now tried to do the following. I set up a class named Auto (which is car in german).


This car class has got a instancevariable of type int which is set to 0. If you call the method accelerate on an object which is created, the objects variable speed is increased by 1.

To set up an object I used a "testclass" named Autotest:


What is the test class doing: Well as far as I understood this correct, the Autotest-class creates an object out of the class Auto named volvo. Then the method accelerate is run on the object volvo and only there. So if I got another object (ford vor example) ford has still a speed of 0. Correct?

Some other questions:
I have to compile Auto.java and Autotest.java. When I start Autotest, how java knows that it has to call the Auto (file). Is this triggered by Auto volvo = new Auto() -> Auto() = the file Auto (which I compiled out of Auto.java)?
If you set up a class, with lots of methods, is a method always set up by the lines (for example method jump and method sprint):
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

Daniel Lewis wrote:
What is the test class doing: Well as far as I understood this correct, the Autotest-class creates an object out of the class Auto named volvo. Then the method accelerate is run on the object volvo and only there. So if I got another object (ford vor example) ford has still a speed of 0. Correct?

Correct. Since speed is an instance variable, each Auto/Car will have its own speed.
Daniel Lewis wrote:Some other questions:
I have to compile Auto.java and Autotest.java. When I start Autotest, how java knows that it has to call the Auto (file). Is this triggered by Auto volvo = new Auto() -> Auto() = the file Auto (which I compiled out of Auto.java)?

Your classpath tells Java where it should find the Auto class. Java will get the compiled Auto file and use that to determine what attributes and behaviours (methods/functions) are associated with the Auto class.
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
I'll echo what someone else said: type in, compile and run as many code examples as possible. We learn by doing and by making mistakes.


Costs matter. Justice lies in processes not outcomes. Crime is caused by criminals.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
Thomas Kennedy wrote:. . . We learn by . . . making mistakes.
Agree. It is worth changing code and seeing what happens. You learn lots like that, too.

Also remember it takes a lot of time to learn something worthwhile.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Daniel Lewis wrote:If you set up a class, with lots of methods, is a method always set up by the lines (for example method jump and method sprint):


If you're asking whether there is a convention for formatting the code in a method, then yes, there are actually a couple common conventions. The two most common are these:

In the first version, the opening and closing braces are left-aligned under the method signature. In the second, the opening brace follows the method signature on the same line. Both conventions observe successive levels of indentation; although these are not required for the code to work, they make reading it much easier. I have never seen real-world code formatted as you have done the sprint method, with the closing brace on the same line as an instruction. But it is legal to write it that way.

Note that, provided all the required syntactical elements are present and correct, the code will compile and run even crazily formatted code such as:

But you shouldn't write your code that way. No one will want to look at it.

Hope that helps.
Daniel Lewis
Greenhorn

Joined: Dec 20, 2011
Posts: 10
Hello again! Thank you for the replies. I see I have to do a lot of practice :-)

I saw that I wrote "void accelerate()" in the first example and "public void jump()" in the second one. I also noticed that I don't know the difference between these 2 lines of code. (void in comparision to public void).

I rebuilt the "Car-Example". I set up a car-class (car.java):


and a CarStarter-Class (CarStarter.java):


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4376
    
    8

You've got a class called car. You've created an instance of this called ford. Fine so far.

Then, what you're probably intending to do is speedup and slow down the instance you've just created. But that should be ford.speedup() and ford.slowdown(). Instead you've used car.speedup().

ClassName.methodName() is how you call a static method methodName belonging to the class ClassName. So your code is trying to treat speedup() and slowdown() as static methods (meaning they belong to the class, not to individual objects). Since they aren't static, the compiler won't let you do this. It's insisting that you call them on an instance of the class.

(Part of the problem is that you're not following Java conventions here. Classes should begin with a capital, in which case this would be Car, and the situation would be a little more obvious to read).

Edit - of course, my answer makes no sense now because you've edited your post. Don't do that, please!
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Daniel Lewis wrote:I saw that I wrote "void accelerate()" in the first example and "public void jump()" in the second one. I also noticed that I don't know the difference between these 2 lines of code. (void in comparision to public void).

The difference is the level of access. One has public access, and the other has package-level access.

There are four elements of a method signature: access modifier, return type, name, and parameters. Neither accelerate nor jump takes any parameters; if they did then they would be enumerated within the parentheses. And neither returns a value, therefore both use the void keyword. The jump method is public. That means that it can be invoked from any other class in any other package. The accelerate method has no access modifier, so it has the default access which is package level. Classes in the same package can invoke this method, but Class outside the package it will not even know it exists.

Btw, I hope you are feeling less discouraged. It seems to me you are doing just fine.
Daniel Lewis
Greenhorn

Joined: Dec 20, 2011
Posts: 10
@Matthew:
Edit - of course, my answer makes no sense now because you've edited your post. Don't do that, please!

Sorry for this, i reedited it, so it makes sense again. :-)

@Dennis: Thank you for your "push" :-). I think I have to give up the illusion, that I will "make" a real usable program in short time. Probably I am pushing myself too hard... In fact, it might be the best to keep it simple and practice OOP. It is really not easy to me and for me. :-)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
Why are you writing speed = --speed; and similar? You can simply write speed--; as a line to itself.
Daniel Lewis
Greenhorn

Joined: Dec 20, 2011
Posts: 10
Thank you for the tip :-)
Chris Rothburn
Greenhorn

Joined: Dec 14, 2011
Posts: 15
I am also finding "Head First Java" a bit hard to follow, as they use terms they have not yet explained in certain code examples, and are not exactly clear on the uses and purpose of much of the syntax they present.

I've found that if I just keep reading, then go back after I have absorbed later material, the points become more clear. But read and reread make for slow progress. I suspect there are books that make learning Java easier, but this is what I have so I am trying to work with it. One option I am contemplating is also using some of the on-line tutorials available. I suspect having different authors explain concepts in different ways may make the learning process easir, but fear it may just lead to confusion.

Anyway, as another beginner at the same point in the learning process, using the same material, I certainly sympathize with your frustration at the unclarity of the text...
Daniel Lewis
Greenhorn

Joined: Dec 20, 2011
Posts: 10
Anyway, as another beginner at the same point in the learning process, using the same material, I certainly sympathize with your frustration at the unclarity of the text...


I am still round page 38 :-), trying to get the OOP in my head. It is really hard, because - as you said in other words - it seems I am not getting forward ... Just thought I look for some userinteraction (Userinput) but then I saw, that it is NOT a simple Input construct (like Python for example).

However I am not giving up. I really want to learn Java, but it is really hard and frustration is coming back again and again.
Daniel Vlad
Greenhorn

Joined: Aug 07, 2011
Posts: 29
Related to understanding the basic concepts of OOP, you could also try this little tutorial: http://sepwww.stanford.edu/sep/jon/family/jos/oop/oop1.htm . It helped me out quite a bit with understanding what objects are, what methods are and so on and how to relate them to the real world (which in my opinion is very important to do when you are at the beginner level).

Also , glad to see you are not giving up i struggled with the same things you do now but i managed to get the general idea after a while and the HeadFirst series has helped and is still helping me a lot.
Chris Rothburn
Greenhorn

Joined: Dec 14, 2011
Posts: 15
I certainly learned more from that, in a fraction of the time, than I did from the first couple of chapters of Head First Java!

Some people just have the knack of presenting concepts. Silliness can work.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Each and everything in life needs some kind of hardwork....Just stick to it read it twice ,thrice and you will see the differnce


http://www.lifesbizzare.blogspot.com || OCJP:81%
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7718
    
  20

Daniel Lewis wrote:I am still round page 38 :-), trying to get the OOP in my head. It is really hard, because - as you said in other words - it seems I am not getting forward ... Just thought I look for some userinteraction (Userinput) but then I saw, that it is NOT a simple Input construct (like Python for example).

I/O is a major PITA with Java - arguably it's worst failing - it's complex, arcane, has several gotchas, and requires you to learn a lot of new classes at once. They've tried to make things easier with the java.util.Scanner class, but personally, I don't find it any better.

The classic newbie question is: why can't I just write something like:
int myInt = System.input("Please type in an integer");
and they may get around to it at some point (although it'll probably be put in the Integer class), but don't hold your breath.

Once you get over the initial learning hump, you'll find that all those classes allow you to do a dizzying amount of stuff with files, pipes, streams, and anything else you can think of to pass information around between programs.

Think of it like a bicycle: a bit tough to master at first, but once you're on you never forget how.

Good luck.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Java too difficult for me? Trying to learn with Head first Javabook