• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a Shortcut Method for Object Invoking  RSS feed

 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm trying to create a shortcut for invoking objects, and this is what I came up with:This does not work, as a String can not be the name of an object for some reason... or can it?

So do you know of a way that I can make this happen? I mean, do I need to change the variable-type in order to make this work?

Why am I trying to do this?
I'm trying to shorten my code, as well as allowing for quicker and more fluent coding on my part.
Also, this is referred to as 'invoking an object', correct? (ClassName objectName = new ClassName();)
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's ignore the how and focus on the why for now.
How is

a "shortcut"* as compared to

You still end up with two lines of code
*I presume shortcut means less lines of code
 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'Shortcut' as in less characters.

Fromto

Although it'd be nice to be able to do something like this:So I guess the method would look something like this...
What I'm really wondering is if this is even possible, and if so, how (do I simply need to put in the correct data-types?)

But you see, the object(call) isn't to be used as both in most cases, but rather it's to invoke the object to be used throughout a method... because in my program's methods I am using switches quite often.

So as you can see, the whole '2 lines' issue really isn't what I'm 100% focused on, it's the invoking line and making it shorter.
* yes, I've already managed to do this as an alternative for the whole scanner line, making it less of a pain to create scanners
 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I just figured out Object is a data-type (or is it called a reference-type?), then I tried replacing data-type with Object in the program that was shown in my previous post, and still nothing - aside from the error saying variable objectName has already been declared in object method, which leads me to believe that objectName should be a different type, but I don't know what...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Levi Neuxell wrote:'Shortcut' as in less characters.

Levi, that should be the LAST thing on your mind.

Since you're posting threads in the Beginners' section, I assume that's what you are; so why are you trying to come up with a new way of writing Java? Learn how to do things properly before you embark on "shortcuts".

From the look of it, you're looking for some sort of generic way of creating and calling an object and its methods; and it already exists: It's called 'reflection' and, almost certainly, your course hasn't covered it yet because you're not ready for it.

Furthermore, it's complex, difficult to understand, even more difficult to test, and SLOW (up to 50 times slower than regular Java code). In 12 years of writing Java, I can still count the number of times I've used it for more than just trivial stuff on the fingers of both hands; and I go a long way to avoid it if I can.

For the moment, concentrate on writing clean simple normal code, even if it involves exercising a few more carpal muscles.

Winston
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure you use the correct terminology.
Levi Neuxell wrote:So I'm trying to create a shortcut for invoking objects, ...
Also, this is referred to as 'invoking an object', correct? (ClassName objectName = new ClassName();)

No. You don't "invoke objects". You mean: create objects. You can only invoke methods.

Levi Neuxell wrote:This does not work, as a String can not be the name of an object for some reason...

Objects do not have names. Variables have names.

Your solution doesn't work, because in Java, arguments are passed by value, not by reference. In other words, you don't pass the variable call itself to your method named object; you pass only the value of the variable to the method. The objName parameter of method object will contain a copy of the value of the variable call. Modifying objName does not modify call.

When you want to introduce a new variable, you will have to declare it. There is no way around that. You can't declare a variable call by just specifying its name in a method call, such as object(call); - there is no way around the fact that you have to declare it by specifying the type and name of the variable: CallName call;

It seems you are confused about the difference between variables and objects.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!