faqts : Computers : Programming : Languages : Java

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

14 of 24 people (58%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

Java: Class: Create: Simple: How to create a class in Java?

Sep 30th, 2003 09:13
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 27 September 2003 - 02:00 - 02:24 am ----------

Java: Class: Create: Simple: How to create a class in Java?

A class is zero or more functions, procedures or variables,
grouped together, with a common name (e.g. addressC).

---

Inside you tell about their access (e.g. private, public,
protected or private protected).

http://www.faqts.com/knowledge_base/view.phtml/aid/24759/fid/165

---

There are also zero or more special procedures to initialize
(called 'constructors').

---

Note:

specific user controlled destructors do not exist in Java.

---

This class information will be stored in 1 specific location in memory.

---

To use your class, you first create and assign it to some variable.

---

Note:

This process of creating you can repeat zero or more times, and each
time a new location in memory with the same memory structure as that of
the given class (in that single memory location) will be assigned to
the variable associated with that class.

So all this variables can be seen as initially having the same
structure (=variables, and a reference to the 1 memory location where
that functions and procedures can be found) as that class, but later
possibly with different data (=variables) inside it, and this variables
are also rather independent of each other (as each variable has its own
separate location in memory).

---

To access the procedures, functions and variables, after
initializing, you can use the classname followed by a dot
followed by the name of that variable, function or procedure.

---
---

Steps: Overview:

 1. -Create a new application in Java

 2. -add the following code about
     your class:

      class addressC {
       //
       private
        //
        private String nameS;
        private String streetS;
        private String cityS;
        private String countryS;
        private String telephoneS;
        private String emailS;
        //
        public addressC( String nameS, String streetS, String cityS, 
String countryS, String telephoneS, String emailS );
        public void PROCView();
      }

 3. -Then give the code for the procedures or functions used in that 
class:

      // a constructor for this class
      addressC.addressC( String nameS, String streetS, String cityS, 
String countryS, String telephoneS, String emailS ) {
       this.nameS = nameS;
       this.streetS = streetS;
       this.cityS = cityS;
       this.countryS = countryS;
       this.telephoneS = telephoneS;
       this.emailS = emailS;
      }

      // view current values of variables in this class
      public void addressC.PROCView() {
       System.out.println( nameS + ' ' + '-' + ' ' + streetS + ' ' + '-
' + ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + 
telephoneS + ' ' + '-' + ' ' + emailS );
      };

http://www.faqts.com/knowledge_base/view.phtml/aid/16104

 4. -Declare the variables which you intend to use with your class:

      addressC knudO = new addressC( "myname", "mystreet 
1", "mycity", "mycountry", "01-2345678", "myemail@address" );

      addressC vanessaO = new addressC( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );

 5. -Supply some source code where you use your class:

      public static void main( String args[] ) {
       // initialize a variable representing that class:
       addressC knudO = new addressC( "myname", "mystreet 
1", "mycity", "mycountry", "01-2345678", "myemail@address" );
       addressC vanessaO = new addressC( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );
       // do something (e.g. show the current content stored in that 
variable:
       knudO.PROCView();
       // finally release the memory used by that variable, after use, 
when you do not need it anymore
       }

 6. -That will give alltogether the following source code in your Java 
program:

     class addressC {
      //
      private String nameS;
      private String streetS;
      private String cityS;
      private String countryS;
      private String telephoneS;
      private String emailS;
      //
      // a constructor for this class
      public addressC( String nameS, String streetS, String cityS, 
String countryS, String telephoneS, String emailS ) {
       this.nameS = nameS;
       this.streetS = streetS;
       this.cityS = cityS;
       this.countryS = countryS;
       this.telephoneS = telephoneS;
       this.emailS = emailS;
       System.out.println( "address object is created and then 
initialized" );
      }
      // view current values of variables in this class
      public void PROCView() {
       System.out.println( nameS + ' ' + '-' + ' ' + streetS + ' ' + '-
' + ' ' + cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + 
telephoneS + ' ' + '-' + ' ' + emailS );
      };
     }

     class testaddressC {
      public static void main( String args[] ) {
       // initialize a variable representing that class:
       addressC knudO = new addressC( "myname", "mystreet 
1", "mycity", "mycountry", "01-2345678", "myemail@address" );
       addressC vanessaO = new addressC( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );
       // do something (e.g. show the current content stored in that 
variable:
       knudO.PROCView();
       // finally release the memory used by that variable, after use, 
when you do not need it anymore
      }
     }

 7. -When you run this source code, you will see a message with that 
address information:
     (save this stand alone application as 'testaddressC.java')

http://www.faqts.com/knowledge_base/view.phtml/aid/24176/fid/165

      javac.exe testaddressC.java

      java testaddressC

       address object is created and then initialized

       address object is created and then initialized

       myname - mystreet 1 - mycity - mycountry - 01-2345678 - 
myemail@address

      Press any key to continue . . .

---
---

Internet: see also:

C#: Class: Create: Simple: How to create a class in C#?
http://www.faqts.com/knowledge_base/view.phtml/aid/24304

C++: Class: Create: Simple: How to create a class in C++?
http://www.faqts.com/knowledge_base/view.phtml/aid/24828/fid/163

Delphi: Class: Create: Simple: How to create a class in Delphi?
http://www.faqts.com/knowledge_base/view.phtml/aid/24789/fid/175

----------------------------------------------------------------------