Results 1 to 4 of 4
- 02-16-2013, 04:28 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Classes, constructors and methods
Hi, i'm new to programming and chose Java to be the one. I've been studying it from numberous sites but i'm still stuck on some definitions.
For my understanding a Class has, at least, a constructor that creates instances of the class and have no return value, and a method to provide the behaviour of the instance. Is that right?
I'm confused about the syntax of constructors and methods:
Example: Class Dogs{
/**class fields*/
String name;
String owner;
/**constructor*/
public Dogs(String dogName; String dogOwner){
name=dogName;
owner=dogOwner;
}
/**methods.....*/
}
Is it right? The dog can have same names but different owners or have the owner but different names. If not it is the same instance isn't it? My doubt is also in the sintax inside a constructor: nameOfTheClass variable = new nameOfTheClass. Why isn't it used all the times?Last edited by GRGoucho; 02-16-2013 at 04:30 PM.
- 02-16-2013, 04:31 PM #2
Re: Classes, constructors and methods
Recommended reading: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-16-2013, 10:37 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Re: Classes, constructors and methods
Thanks but i've been reading it and others...
I'm looking for an user's point of view as it seems I cannot really understand the explanations i've seen. I've been reading more in a good book (i think) and learned a little bit more of what an object really is and where it came from. It seems an object is a very well encapsulated structure that is defined by its by its variables and private methods and can communicate (interact) with other objects also on methods. This is my understanding of it. I'm trying to understand it's logic before i can get to constructors although i have an idea of them.
- 02-17-2013, 06:54 PM #4
Member
- Join Date
- Feb 2013
- Location
- Islamabad, Pakistan
- Posts
- 25
- Rep Power
- 0
Re: Classes, constructors and methods
Ok listen Class have two things data members(mean variables) and member funtions (mean methods or funtions).... The name of your constructor should be same as the name of your class. e.g
Java Code:class Test { int a; float b; Test() { a=0; b=0.0; } }
Now you can use private or public with classes but at the moment leave it...
Constructor have no return type....
Now what are member funtions.....
Funtions that use your data members now if you want to set value of a nd b at run time then there will be two set funtions like
Java Code:public void set_a(int x) { a=x; } public void set_b(float y) { b=y; }
Now what is object?? in main funtion you will have to do Test obj=new Test();
Now in this line Test is you class name and it will be data type of object which means that every object of this classs will have two variable int a and float b..... now when you will create another object it will have its own two same variables... further obj is name of object you can change it..... new means object is being created at heap means at run time ... Test() is your constructor which gives 0 and 0.0 to a nd b respectively....
Hope you understand it.....
Similar Threads
-
Printing the superclass/constructors/methods of a class
By tehsumo in forum New To JavaReplies: 11Last Post: 03-05-2012, 11:18 AM -
Constructors & Methods
By candygirl198827 in forum New To JavaReplies: 1Last Post: 12-02-2010, 01:51 AM -
Formulas in methods or constructors?
By kyameron in forum New To JavaReplies: 11Last Post: 11-20-2010, 12:22 PM -
Calling subclassed methods from constructors
By arefeh in forum New To JavaReplies: 7Last Post: 01-22-2010, 12:22 AM -
Constructors Objects and Classes
By Tykk in forum New To JavaReplies: 4Last Post: 10-10-2009, 11:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks