Results 1 to 6 of 6
Thread: Datatype of a class?
- 10-10-2011, 11:16 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 7
- Rep Power
- 0
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:
What can I do with the ''vehicle'' datatype I typed in JustAClass?Java Code:class JustAClass { Vehicle haha; //Why is this posible?! What can I do with it? } class Vehicle { public static void main(String args[]) { } }
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.
- 10-11-2011, 03:36 PM #2
Member
- Join Date
- Feb 2011
- Location
- Ahmedabad
- Posts
- 36
- Rep Power
- 0
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-.gif)
- 10-11-2011, 06:54 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 7
- Rep Power
- 0
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.
-
Re: Datatype of a class?
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.
what do you mean "I can't declare the variable in the sample class"?I cant declare the variable in the sample class.
- 10-12-2011, 06:45 AM #5
Member
- Join Date
- Feb 2011
- Location
- Ahmedabad
- Posts
- 36
- Rep Power
- 0
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-.gif)
- 10-12-2011, 07:04 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 7
- Rep Power
- 0
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
-
how to plot a curve which has double datatype coordinates
By murdplacid in forum Java 2DReplies: 6Last Post: 11-02-2010, 04:14 PM -
char datatype
By frejon26 in forum New To JavaReplies: 5Last Post: 05-04-2010, 11:21 PM -
how to find a datatype
By hasysf in forum New To JavaReplies: 1Last Post: 09-06-2009, 10:41 AM -
Problem in using Object datatype
By mfaizan24 in forum New To JavaReplies: 6Last Post: 05-05-2009, 11:51 PM -
Problems with a complex datatype in a webservice
By lichtbringer in forum Web FrameworksReplies: 2Last Post: 10-29-2008, 05:32 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks