20096 total geeks with 3178 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
Domuk
No, not an issue with the PHP - I was responding to "AJAX not being cross site is annoying"
MaxMouse
Really? i thought that would only be important if the user had some kind of control over where the XML came from, if you hard code it (As in a PHP file) wouldn't that eliminate XSS attacks?
Domuk
Yes, but very, very necessary. AJAX requests run in the context of the browser, there'd be no security if it was cross-domain .
MaxMouse
AJAX not being cross site is annoying, all other scripts can be used in that way, having to resort to PHP to patch it is a shame.
SAJChurchey
thx MaxMouse

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


News Feeds
The Register
FDA takes aim at
illegal net
pharmacies
Oman cuffs 212 for
selling VoIP calls
IBM chase HP (and
Sun) with tiny mem
prices
Hackers free Snow
Leopard from
Jobsian cage
MySpace makes peace
with Indies
Nvidia previews
next-gen Fermi GPUs
Potty-mouths
charged for Comcast
hijack
Microsoft
Silverlight - now
with hidden Windows
bias
Apple cult leader
emails outside
world
Sony demos monster
3D TV
Slashdot
New Microsoft
Silverlight
Features Have
Windows Bias
How Heavy Is the
Internet?
Anti-Smoking
Vaccine Is Nearing
the Market
iPhone Owners
Demand To See Apple
Source Code
Proton Beams Sent
Around the LHC
Microsoft"s Lack of
Nightly Builds For
IE
Some Claim Android
App Store Worse
Than iPhone"s
Climatic Research
Unit Hacked, Files
Leaked
Aging Nuclear
Stockpile Good For
Decades To Come
Netbooks Have
Higher Failure Rate
Than Laptops
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 379 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.

Related Links:
Writing Your Own Exceptions
This second article in my series on exceptions in Java will introduce you to the finally block...
Wireless Java - Developing with J2ME
A review of the book titled Wireless Java, Developing with J2ME by Jonathan Knudsen, published via Apress.If somebody were to ask me, Which technologies have had the biggest impact on society in recent times?, it is likely that the Internet and mobil..
Catch Jackrabbit and the Java Content Repository API
The Java Content Repository API (JCR) promises to greatly simplify Java database programming. Content repositories extend databases with versioning, rich references, workspaces, extensible information models, and other features. This article reviews ..
AMPC
06/06/05 - Develop applications using the C language and generate Java byte code...
Three Minutes to a Web Service
This short tutorial on writing a Java Web service with the next-generation JAX-RPC 2.0 API demonstrates how Java annotations simplify Web service development...
Recent JDK Features Ease Web Service Development
The Java API for XML-based RPC (JAX-RPC) 2.0, currently in early draft review in the JCP as JSR 224, promises to vastly simplify RPC-based Web service development. This article reviews key JAX-RPC 2.0 features, and highlights how taking advantage of ..
Mapping the Java Landscape
In this short editorial, Bill Venners explains the context and motivation behind Artimas new online magazine, Leading-Edge Java, and describes the focus and flavor of the articles it will publish...
ADVANCED macromedia COLDFUSION MX 7 Application Development
With a slew of advanced new features, ColdFusion MX 7 represents both an opportunity and a challenge for those developing Web apps with it. The opportunity, of course, lies in taking their applications to new and increasingly sophisticated levels. Th..
Netbeans 4.1 Released
njcoder writes Netbeans 4.1 was released a few days ago. Though it is only a short time since 4.0 was released and only a minor version number increase, the new Netbeans 4.1 contains a number of significant enhancements. New features include enhanced..
OpenOffice, Free-Software Supporters Make Peace over Java
The FSF, Sun and OpenOffice.org are moving to reconcile their differences about the use of Suns Java in OpenOffice 2.0...
Beginning Java: Setting up JDK
Writing Your Own Exceptions
Exception Handling in Java
[Bahasa Indonesia] Thread Java
[Deutsch] PopUp-Fenster mit Javascript
Java Threads
Java Applet Security


     
Your Ad Here
 
Copyright Open Source Institute, 2006