Results 1 to 6 of 6
  1. #1
    Skipito is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default Datatype of a class?

    Hello,

    What does a datatype of a class mean? What can I do with it? I have seen many datatypes that have the class' name instead of int or something similar. I also have seen arrays with as datatypes the class' name..

    For example this:

    Java Code:
    class JustAClass {
        
      Vehicle haha; //Why is this posible?! What can I do with it?
       
    }
    
    class Vehicle {
    
        public static void main(String args[]) {
     
           
        
     }
    }
    What can I do with the ''vehicle'' datatype I typed in JustAClass?

    I did a Google Search, and unfortunately didn't find enough helpful information. Does anybody know the name of this ''using a class as datatype''?

    Thank you.

  2. #2
    Join Date
    Feb 2011
    Location
    Ahmedabad
    Posts
    36
    Rep Power
    0

    Default Re: Datatype of a class?

    Hi Skipito

    when you create a class it is a custom datatype then you can create variable(object) same as datatypes like int,Integer and so on...
    So Vehicle written in your class is a class written some where else.

    as if i create class myDataType then i can use myDataType class as follow

    Java Code:
    public class myDataType{
    int a;
    }
    public class sample{
    public static void main(STring a[]){
    myDataType a;
    }
    }
    Hemant Metalia
    cool

  3. #3
    Skipito is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default Re: Datatype of a class?

    Thank you very much for your reply.

    However, I still don't get the 'use' of this code. I mean, what can you do with the 'Datatype a' now? I cant declare the variable in the sample class.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Datatype of a class?

    Quote Originally Posted by Skipito View Post
    However, I still don't get the 'use' of this code. I mean, what can you do with the 'Datatype a' now?
    You can use it in the JustAClass's constructor and instance methods once you've instantiated the haha variable either by having it refer to a new Vehicle instance or some other already existing instance. Right now haha has been declared but not constructed yet and so it refers to nothing; it is null.


    I cant declare the variable in the sample class.
    what do you mean "I can't declare the variable in the sample class"?

  5. #5
    Join Date
    Feb 2011
    Location
    Ahmedabad
    Posts
    36
    Rep Power
    0

    Smile Re: Datatype of a class?

    Hi Skipito

    First of all you need to know why we create a class ?
    we create a class to combine variables and operations on it.

    as example if you want to create rectangle you need x and y axes so combine them to a class rectangle and then operations like draw rectangle,moverectangle etc...


    Java Code:
    class Rectangle{
         long x;
         long y;
         public rectangle(long x1,long y1){
                x=x1;
                y=y1;
         }
         public void drawRectangle(){
          //code to draw rectangle
         }
         public void moveRectangle(){
          //code to move rectangle
         }
    
    }
    
    //now you can use rectangle datatype and its operations
    
    public class sample{
              public static void main(String a[]){
                          Rectangle r1=new Rectangle(5,50);
                           r1.drawRectangle();//this will draw a rectangle
                           r1.moveRectangle();//this will move the rectangle
                 }
    }
    Hemant Metalia
    cool

  6. #6
    Skipito is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default Re: Datatype of a class?

    Thanks for the comments.

    So basically, the above code where you say: ''Rectangle r1'', is where you declare a object for the class Rectangle? Right?
    So what I did in my example in the first post: ''vehicle haha'' is declaring an object called haha for class vehicle right?
    So basically its connecting 2 classes via objects?

    Thanks mates!!!

Similar Threads

  1. Replies: 6
    Last Post: 11-02-2010, 04:14 PM
  2. char datatype
    By frejon26 in forum New To Java
    Replies: 5
    Last Post: 05-04-2010, 11:21 PM
  3. how to find a datatype
    By hasysf in forum New To Java
    Replies: 1
    Last Post: 09-06-2009, 10:41 AM
  4. Problem in using Object datatype
    By mfaizan24 in forum New To Java
    Replies: 6
    Last Post: 05-05-2009, 11:51 PM
  5. Problems with a complex datatype in a webservice
    By lichtbringer in forum Web Frameworks
    Replies: 2
    Last Post: 10-29-2008, 05:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •