22858 total geeks with 3297 solutions
Recent challengers:
best bread maker
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
sefo
anilg, new comments are deleted automaticall y because of some abuse recently
anilg
this is plain wierd. I submitted comments twice to article 950, and they dont seem to be there. Something wrong with the comment code?
CodeX
shout-boxes in general are old + the staff thing happened to everyone after an issue 2 months ago
anilg
/me is no longer staff :(
anilg
Also, osix's shoutbox predated twitter. Heh.

Donate
Donate and help us fund new challenges
Donate!
Due Date: Jul 31
July Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
UK.gov sticks to IE
6 cos it"s more
"cost effective",
innit
T-Mobile UK pumps
out the iPhone 4
Polaroid 300
instant print
camera
NatWest dumps O2
Money
YouTube ups video
time limit
Alleged expenses
fiddlers to face
justice
Nude trampolinist
bounces free from
court
Nexus One phone
rockets to 28,000ft
UK.gov drops £6m on
Google
Fake Firefox update
used to sling
scareware
Slashdot
British ISPs Favour
Well-Connected
Customers
"Bizarre"
Nanobubbles Found
In Strained
Graphene
1-in-1,000 Chance
of Asteroid Impact
In
...
2182?
2 Chinese ISPs
Serve 20% of World
Broadband Users
World"s Fastest
Hybrid OK"d For
Production
Sometimes It"s OK
To Steal My Games
Thermoelectrics
Could Let You Feel
the Heat In Games
KDE SC 4.7 May Use
OpenGL 3 For
Compositing
Perl 6, Early, With
Rakudo Star
Internal Costs Per
Gigabyte —
What Do You Pay?
Article viewer

Class



Written by:manila
Published by:Nightscript
Published on:2009-10-08 03:59:29
Topic:Java
Search OSI about Java.More articles by manila.
 viewed 1596 times send this article printer friendly

Digg this!
    Rate this article :
A brief explanation of Class in Java. What is it?

Class

A class in Java is much like one in C++. It consists of a group of related methods and variables lumped together under one name. The static class variables are for class-as-a-whole data. They are allocated only once at load time and are shared by all instances of objects of that class. The instance variables, are allocated inside each object of that class. Static class methods work when there is no current object. They can only reference static class variables and static methods, unless of course they allocate an object and then use explicit references to the instance variables. Instance methods work by default on the fields of the current this object.

Let’s say you had a TV class to deal with television sales. There are two kinds of variables:

static variables that track facts about all televisions in general, e.g. total sold or a list of manufacturers.

instance (non-static) variables that track facts about individual televisions, e.g. manufacturer, serial number, diagonal, type LCD/CRT etc.

The facts about televisions in general are stored in static variables, only one copy of the variable for the whole TV class. The facts about individual televisions are stored in instance variables TV objects instantiated with new. There is one copy of each instance field per TV object.

Similarly, there are static methods about televisions in general and instance methods about particular televisions. Unless you have a TV object, you can’t call any of the instance methods. You don’t need a TV object to call one of the TV static methods. static methods may only look at the static fields. Instance methods may look at either the static or instance fields.

There can be only one public class in each source file. If your class was called HelloWorld the name of the source file must be precisely HelloWorld.java with every letter exactly matching even in case. In Java, there is no such thing as a method or variable that does not belong to some class. Java comes with a built-in set of classes arranged in a class hierarchy.



Class vs Object

One of the fundamental things a newbie has to understand is the difference between a class and an object. It is very simple once you get your head around it. The class is like a blue print for a house, but the object is like a house itself. Using the same house blue print, you can create many instances of the house, each slightly different.

In addition, a house class maintains static information common to all house object instances. A house object contains its own instance information which may differ from other house objects. The code for the methods is attached to the class because the the code is the same for all house objects. The house class survives until the program ends.

However, the house objects are garbage collected after there no longer exist any references (pointers) to them. Classes come into being when you first use one of their methods, either a static method or a constructor. They are loaded into memory and stay there. Objects come into being when you instantiate them by using new to invoke a constructor.

Did you like this article? There are hundreds more.

Comments:
CodeX
2009-10-08 17:07:00
classy article
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
goldie
using javamail i am getting excetion like this on Tue 5th May 6am
avax.mail.MessagingException: Unknown SMTP host: pop3.gmail.com; nested exception is: java.net.UnknownHostException: pop3.gmail.com at com.sun.mail.smtp.SMTPTransport.openServ er(SMTPTransport.java:1543) at com.sun.mail.smtp.SMTPTransport.protocol
kdemetter
Blog entry for Wed 11th Apr 1pm on Wed 11th Apr 1pm
lately i've been playing with Java a little . I'm working on a application that can create a list of meals for one week from a large list of meals . well , it works . just some problems with Swing not doing what i want it to do :-) http://www.os

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Java Programming - Level 1 by D-Cypell

A first level Java programming test. The test contains 10 questions on Java syntax, concepts and the API.
History and Basic Programming Knowledge by HackMaster321

A test of your knowledge of java history and programming. Tests your ability to recall java historical facts, and remember what some of javas more common commands and syntax are and do.


     
Your Ad Here
 
Copyright Open Source Institute, 2006