• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class instantiating itself

 
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class is XCopy.   inside of the main method it is instantiating the XCopy and giving it the reference variable x.  
I keep seeing this in examples.  I don't know what this is used for.



Thanks,

Kevin
 
Marshal
Posts: 79405
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That isn't the class doing anything, but one of its methods instantiating it.
Yes, that is a common practice; you have a class whose objects have methods to run your application. You probably only need one object, and you create that instance in the main() method. You can see examples in our FAQ.
Be careful. In that example you are creating one object. But if your object then creates another object of the same type, and that object creates a third, you can suffer an infinite regression. It won't go on forever; it will run out of memory and crash.
 
Saloon Keeper
Posts: 27861
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost every stand-alone Java app I wrote looks something like this:

There are a number of reasons why this is preferable to simply running everything in the main() method. For example, "args" might be fairly complex, so I might parse them out (possibly using a standard parameter parsing library) and store them in a runtime arguments object which is then made a member property of the "foo" instance.
 
Marshal
Posts: 28260
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


One thing I've noticed here is that beginners are prone to inventing new (non-existent) rules because they haven't figured out how to focus yet. When you focus, you can see that the line of code there creates an object of class XCopy and assigns (a reference to it) to a variable of the same type. And that's all. There isn't a rule about what context you can use it in, apart from the rules which say where you can use a declaration like that. And there certainly isn't a rule which says it works differently if you use it inside class XCopy. Focus.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,

I read the FAQ link.  I see what it is doing until it gets to the last part where it says "Alternative"  .

I can tell that it does the same thing as above but all in one class.  It a class instantiating itself still seems strange to me.  But since it is used all over the place and I've seen it work I'll just know that it is OK to do it like this.

Tim,
I think that I'd use your code as a starting point.  

Paul,

Is it possible that I am focusing too much on this?  To me it's like thinking about the concept of infinity.  There is no rule about the class making itself into an object, but it seems strange.  

Thanks,

Kevin

 
Campbell Ritchie
Marshal
Posts: 79405
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can ignore “Alternative” since even Winston says he doesn't really like it. As I said, it is common practice to create an instance of the same class.

Please don't write long comments with // something. Use /* comments */ instead. You had too long a comment in your first post and that long line has adversely affected the formatting in every post on this thread.
 
Paul Clapham
Marshal
Posts: 28260
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:Is it possible that I am focusing too much on this?  To me it's like thinking about the concept of infinity.  There is no rule about the class making itself into an object, but it seems strange.  



Most programming languages (believe it or not) are mostly designed using the principle that their features are as simple as possible. Many of the Java features may look horribly complicated, but they still try to minimize complexity given what they are trying to do. So using "new" to create an object of some class is a pretty simple idea. When you see code creating an object of class X inside class X, then "Hmm, that seems strange" is a reasonable reaction. But it's best to assume that there isn't an extra bit of complexity there and carry on with programming. It may be that there is actually an extra bit of complexity but if that's the case then you'll find out about it later and you can deal with it then.

I think there's two personality types, one will look at a situation and think "What about this possibility" and "What if this thing happens" and so on. The other will think "Well, if those things happen then we'll deal with them at that time". (This is not situations like diving off a 20-meter tower, it's situations where unforeseen consequences are not likely to be bad.) I'm in the second category.
 
Campbell Ritchie
Marshal
Posts: 79405
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . programming languages . . . designed . . . as simple as possible. . . . .

If you search for “Java buzzwirds” you will find Java┬« has “simple” as one of them.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

I have been a tester for a long time.  I usually think the first choice.  I keep thinking "what if" and doing it to see what happens.

If there is a line of people using a terminal or kiosk, I'm the one waiting for there to be nobody in line behind me.  I'll go through every menu and possibility.  

Kevin
 
Tim Holloway
Saloon Keeper
Posts: 27861
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Paul Clapham wrote:. . . programming languages . . . designed . . . as simple as possible. . . . .

If you search for “Java buzzwirds” you will find Java┬« has “simple” as one of them.


Yeah. More accurately, most languages are designed to make whatever their designers want done as simple as possible. Which is why LISP and Java are so different.

Though for all-around use, I like Alan Kay's take: "Simple things should be simple and complex things should be possible".
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,

My goal was to start learning about Exception error handling.  I thought I'd give your starting code a try.   I have errors and after staring at them for 10 minutes, going to dinner and staring again, I don't know what i'm doing wrong.   I put the errors in messages where I'm stuck.  

I know that I need to send some kind of data to doit() I don't know how to use:    String[] args  
Is it an array of strings?  

I'm on the easy stuff and I still get stuck.

package howAboutExceptions;

 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ooh!  class had a capital c.

The errors are gone but I still don't know how to use String[] args

Thanks,

Kevin
 
Saloon Keeper
Posts: 10781
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now try running it with some command line arguments.
 
Marshal
Posts: 4526
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:The errors are gone but I still don't know how to use String[] args


args is just an array of Strings which correspond to arguments provided on the command line when the application was run.  Write some throw-away code to see how it works:
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey and Ron,

I understand what String[] args.  We have this idea in the old .vbs language.

I'll work on getting this to work using the CMD window.  Is there a way to tell Intellij or other IDEs to imitate running from the command line?  

Thanks,

Kevin
 
Ron McLeod
Marshal
Posts: 4526
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:I'll work on getting this to work using the CMD window.  Is there a way to tell Intellij or other IDEs to imitate running from the command line?


My opinion - if your focus is on learning Java, then just run from the command line and don't let the IDE become a distraction.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it works from the command line.  Now I understand it.  

Thanks.

C:\study>cd Java Head First

C:\study\Java Head First>Javac passing_params.java

C:\study\Java Head First>java passing_params.java  How does this work?
Number of args: 4
Arg #0 is: How
Arg #1 is: does
Arg #2 is: this
Arg #3 is: work?
 
Ron McLeod
Marshal
Posts: 4526
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I use the command line for testing, building, source code control, etc., and just the IDE as an intelligent editor.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I"m going too many directions in this thread.  
I am starting a new thread.  
I'll reply to any comments I get on this thread.

 
Tim Holloway
Saloon Keeper
Posts: 27861
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's how "args" works:

Let's take a Java command line:

There are 2 types of parameters being supplied to the "java" command (the JVM). The first set are options for the JVM itself to use. Then you have the executed class name or "-jar jarname". What's left are the application parameters.

The Java command parser, possibly with help from the OS command shell that it was launched in, splits the remaining arguments up as space-separated strings, discarding the separators. So args will be the equivalent of this:
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

java passing_params.java  How does this work?


This should not have worked.  The java command wants the name of the class, not the name of the source file:
java passing_params How does this work?
 
Tim Holloway
Saloon Keeper
Posts: 27861
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

java passing_params.java  How does this work?


This should not have worked.  The java command wants the name of the class, not the name of the source file:
java passing_params How does this work?


It's a recent addition to Java. Give a simple source file name and it will automatically assume that it should compile and execute it. Older JVM's couldn't do that.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.  So the javac command is redundant now.
 
Saloon Keeper
Posts: 15615
366
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

It only works with applications that consist of a single sourcecode file.

For all but the simplest of applications, you still need javac.
 
Ron McLeod
Marshal
Posts: 4526
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Launch Single-File Source-Code Programs feature can also be used in shebang files on Linux (this example named datenow):
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron,  Tim, Norm,

I used Javac to compile and then java to run.

It's good to know that I can leave out the compile for code in a single file.  

Thanks,

Kevin
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic