26277 total geeks with 3498 solutions
Recent challengers:
 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
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

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


News Feeds
The Register
Google builds
crowdsourcing into
new Maps code stack
Google"s Native
Code browser tech
goes cross-platform
Yahoo! to "share
something special"
in New York on
Monday
Adobe"s Creative
Cloud fails at
being a cloud
NASA signs off on
sampling mission to
Earth-threatening
asteroid
US military
welcomes Apple iOS
6 kit onto its
networks
Jailed Romanian
hacker repents,
invents ATM
security scheme
Climate scientists
agree: Humans cause
global warming
MIT takes
battery-powered
robot cheetah for a
gallop
Google research
chief: "Emergent
artificial
intelligence?
Hogwash!"
Slashdot
Canadian Cellphone
Users May Get
Justice Over
Phantom Charges
Wired Writer
Imagines Google
Island
Syrian Electronic
Army Hits Financial
Times Sites, Feeds
Arduino Branches
Out, With a
Plug-and-Program
Robot
After Kickstarter
Record, Pebble
Smartwatch Lands
$15M From VCs
Electronics-Loving
"Crazy Ants"
Invading Southern
US
Intel Rolls Out
"Beacon Mountain"
Android Dev
Platform For Atom
Fed. Appeals Court
Says Police Need
Warrant to Search
Phone
UK Consumers
Reporting
Contactless Payment
Errors
FBI Considers CALEA
II: Mandatory
Wiretapping On
Every Device
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 8560 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
silverfrog
2011-01-20 14:30:34
Hello, thank you for this post, helped me much. Some tips from http://www.tubesfan.com/watch/the-basics-of-java-programming helped me to get the basic glimpse of java but still I have to catch up much, because I am self-student. Good luck for everyone in mastering java.
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 tricks by berry120

Think you really know Java? This won't test you on elaborate and never heard of APIs. It will test you on stuff that isn't always as it seems...
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