• 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: 914
10
  • Number of slices to send:
    Optional 'thank-you' note:
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: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27871
196
  • Number of slices to send:
    Optional 'thank-you' note:
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: 28262
95
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:


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: 914
10
  • Number of slices to send:
    Optional 'thank-you' note:
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: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:
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: 28262
95
  • Number of slices to send:
    Optional 'thank-you' note:

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: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:

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: 914
10
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27871
196
  • Number of slices to send:
    Optional 'thank-you' note:

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: 914
10
  • Number of slices to send:
    Optional 'thank-you' note:
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: 914
10
  • Number of slices to send:
    Optional 'thank-you' note:
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: 10798
86
  • Number of slices to send:
    Optional 'thank-you' note:

Now try running it with some command line arguments.
 
Marshal
Posts: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:

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: 914
10
  • Number of slices to send:
    Optional 'thank-you' note:
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: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:

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: 914
10
  • Number of slices to send:
    Optional 'thank-you' note:
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: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:
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: 914
10
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27871
196
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:

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: 27871
196
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

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
  • Number of slices to send:
    Optional 'thank-you' note:
Thanks.  So the javac command is redundant now.
 
Saloon Keeper
Posts: 15625
366
  • 2
  • Number of slices to send:
    Optional 'thank-you' note:
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: 4533
572
  • Number of slices to send:
    Optional 'thank-you' note:
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: 914
10
  • Number of slices to send:
    Optional 'thank-you' note:
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
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |