Results 1 to 7 of 7
Thread: Accessing class, the right way
- 03-22-2011, 10:40 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
Accessing class, the right way
Hi guys,
My first post:), you all participate in a fantastic forum and i am happy to be apart of this learning experience.
I am fairly new to Java; having played around with it for a few months before now studying it at Uni (have just started). I have completed an assignment in two forms. I have completed it using methods in a procedural fashion, as this is what i am comfortable with. I have also re-done this assignment using classes.
My question is this:
I have called the classes by creating an object (as you do) and simply accessed them in the same fashion as you would a method in a procedural program. for example:
MyClass myClass = new MyClass(arg1, arg2);
This works fine and my program runs without a hitch. But is it this form exceptable? should i instead be returning values from my class arguments. The classes themselves are for printed menus and mathematical equations etc..
I guess im asking this because although this works fine, i get a little warning message on my coded work (eclipse) stating that this local variable is never read. I understand what the warning is saying, i am just looking for ideas on whether the way i have accessed these classes is acceptable or is it frowned upon.
I would appreciate any thoughts on this subject.
Cheers
BM
- 03-22-2011, 10:49 PM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
I am new in Java also.According to my knowledge, MyClass(arg1, arg2) is a constructor having too parameters.In your class definition there must be a constructor receiving two arguments. And a constructor has no return type.MyClass myClass = new MyClass(arg1, arg2);
This works fine and my program runs without a hitch. But is it this form exceptable? should i instead be returning values from my class arguments. The classes themselves are for printed menus and mathematical equations etc..
About the warnings,Can you show them ?
- 03-22-2011, 10:50 PM #3
Yes and no err maybe.
Creating an object and calling its methods is obviously the correct way to go otherwise how would you get the code in the method to execute? However what a method does and what value it returns depends upon what your overall goal is. For example if you have a Circle class you could have a method to calculate the area. Now does that method simply calculate the area of the Circle and store it in an instance variable? Or does the method calculate the area and return it to the caller who can then decide what to do with it? Or should the method calculate the area and then display it? :shrugs: The only person who can answer that is the person writing the code.
- 03-22-2011, 10:53 PM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Yes, this is absolutely correct.
You can then call methods from the class's object you just created like so:
and then it will do the method in class otherClass.Java Code:otherClass.doMethod();
You can also extend the parent class by writing for example:
and then you can just write doMethod(), you won't need to make instances of the Two class (create objects).Java Code:public class One extends Two {
- 03-22-2011, 11:09 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
Thats pretty much what im using, the method is inside a constructor. i create the constructor first as in :
public Something(){
}
and then i use this again with :
public Something(){
filled with method
}
the program works fine, but is this the wrong way to be doing it?
- 03-22-2011, 11:15 PM #6
For starters you cannot have a method inside a constructor. If you mean call a method inside a constructor, then yes you can do that. But as I tried to explain earlier there really isn't a right or wrong way to do things. There are certain standards that people should abide by (class names start with capital letter, method names start with lowercase letter) but at the end of the day if your code compiles runs and produces correct output then it is OK. Of course as you gain more experieince you will learn that some things can be achieved in a more efficient manner.
- 03-23-2011, 01:08 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
accessing a singleton class inside another class
By katturv in forum New To JavaReplies: 1Last Post: 12-08-2010, 06:23 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
An example of accessing outer class from inner class
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 09:01 AM -
Inner class accessing outer class
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 08:59 AM -
Accessing one class from another class through swing
By kbyrne in forum AWT / SwingReplies: 5Last Post: 01-03-2008, 07:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks