aspose file tools
The moose likes Beginning Java and the fly likes Were can I look at the code for java classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Were can I look at the code for java classes" Watch "Were can I look at the code for java classes" New topic

Were can I look at the code for java classes

Shane Miller

Joined: Oct 11, 2009
Posts: 17
I am new to learning Java and was wondering were I can look at the code for java classes and methods I use in my code... To give an example to further explain what I am trying to do I will use this:

Let's say that I am using the JOptionPane or creating a Scanner object.... Were can I actually take a look at the code that is the Scanner class or JOptionPane??

I just find it limiting to not know exactly how these things work.. I mean it's one thing to be able to write code just using already created objects, but its another to understand how they themselves are written. I don't know but I don't think one can really call themselves a programmer if all they are doing is reusing other peoples code, which in essence is all my class in college is teaching me to do. I find it kind of sad that schools are churning out so called programmers that can't really program. I mean am I crazy here? Does anyone agree with me? If a school is only teaching you the syntax of a computer language and how to reuse code that has already been written then to me they aren't really teaching you how to program.

Also, does anyone know what I have to do to learn these types of things? Apparantly my school isnt teaching real programming, which I dont think most schools are nowadays. They are just teaching people to re use already written code, which from a business standpoint I guess is a good thing, I mean people don't want to pay someone to write code when they can just use a class that has already been written and save time and money, but to me I don't think I can call myself a programmer if all I am doing is reusing code. I want to be able to write code that actually creates GUI's, I want to be able to write code that creates scanner objects, or whatever type of object for that matter. I don't want to be able to only re use a real programmers code.

If anyone knows were I can take a look at the actual code for these things I mentioned earlier that would be great. I thought that the code itself would be in the java documentation, but all I can find is explanations of how you can use the classes and how to call them up. I have yet to find the actual code itself for these classes.

Why use Windows when you can step through doors into a whole new world of computing without security holes and instability. Join us, and leave windows behind. Try Linux!!!
David Newton

Joined: Sep 29, 2008
Posts: 12617

The JDK is open source, and I guess I thought most of the source was in the file.

To write and understand the compiler, particularly the JIT, will take some time.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 40459
The file mentioned earlier is usually somewhere in your Java installation directories.
David Houghton

Joined: Jan 11, 2009
Posts: 19
If your using NetBeans, accessing the source code is pretty simple, just hold down the control button and then click on the Object or Method you want to see.

You do need to have the src code download and correctly pointed to, but i think netbeans already does this for you automatically.

If not, you can download the file from Sun, then in netbeans point to your downloaded file via 'Tools Menu -> Java Platforms -> Sources Tab', then click add JAR/Folder and find your downloaded file.
Shane Miller

Joined: Oct 11, 2009
Posts: 17
Thanks alot for that info guys. I just dont see the point in using these things with no understanding of how they actually work.

Having this knowledge can only help to make one a better programmer.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
To understand how core java classes behave you could use the API Documentation.

I tend to think that going through the source code to find implementation details to understand behaviour may not be a good idea and also would mean the abstraction provided in this layer is overlooked.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19110

Shane Miller wrote:I just dont see the point in using these things with no understanding of how they actually work.

Not knowing how something works is actually one of the key features of modern programming. It's called "encapsulation" and the main idea is that the designer of the class will document the public interface of the class -- usually a list of methods and their signatures. So that's the "contract" which the class will implement. If the documentation says that method X will do such-and-such and return some value, then method X should do that. There is generally nothing in the documentation which says how the method will do it. That allows the programmer to write whatever code it takes to implement the method, and also to rewrite that code in whatever way, without affecting code which calls the method.

Now having said that, I don't mean to say you shouldn't look at the source code of the JLabel class. It can't hurt for beginners to look at code written by reasonably competent programmers and in fact I think that's a good thing. However don't assume that what you see in that source code is "how it actually works". It would be quite possible for somebody else to write a completely different implementation of that class and, as long as it followed the specs, that would be how it actually works too.

(I would challenge you to post an explanation of how a television works, or a mobile phone, but nah, let's not go there. )
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 40459
Shall I tell you how a television works?

I have a television with a CRT screen. Friends have TVs with plasma screens. They are examples of encapsulation. You turn the TV on and pictures appear on the screen. I can also watch them on this PC which has a liquid crystal screen (I think).
Interface and contract: turn it on and see pictures.
Implementation: CRT or plasma.
subject: Were can I look at the code for java classes