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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

order of execution

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jesper de Jong wrote:
If you don't understand exactly what the difference is between static and instance variables, then study Understanding Instance and Class Members in Oracle's Java Tutorials.


good to see you,ha thanks ,actually my doubts is just how default value will print for instance variable thats enough,now i understand

Jeff Verdegan wrote:

That's what "default" means. It means the value is there even if we don't explicitly set it.


thanks ok i got

Jeff Verdegan wrote:
No. I cannot explain it any more clearly than that. I don't know how to explain "the first time the class is used" more clearly than "the first time the class is used." If you did not understand that, I don't think there is anything I can say that you will understand.

And what part of it did you not understand anyway? You've been doing this the entire thread. Someone gives you a detailed explanation and you ask for more, but you don't provide any feedback and what you didn't understand. That makes it difficult to help you, and can lead to frustration for those who are trying to help.


i mean i understand whenever class is loaded first then it static variable and static block will initialized automatically without creating object for class and acess directly, small examples which is easily to understand thats why iam asking,k sorry for frustration ,whenever if we compile and run the program class is loaded first after that static variable and static method after that what are the loading (constructors,methods,instance variables,instance block,statments)like the way which order will comes? what are the loaded first and how it allocates or occupy memory in heap or stack for variables,methods and objects
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Section 12.4 and 12.5 of the Java Language Specification explains exactly in what order things are initialized when a class is loaded and when an object is created. (Beware, the JLS is not always easy to read - but it is the definitive reference if you want to know exactly how Java works).
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jesper de Jong wrote:Section 12.4 and 12.5 of the Java Language Specification explains exactly in what order things are initialized when a class is loaded and when an object is created. (Beware, the JLS is not always easy to read - but it is the definitive reference if you want to know exactly how Java works).


yeah its very difficult to read and understand
here below progam compile time error whats the problem even i created object and call it,i think its a nested class or local class?

Myclass.java:9: <identifier> expected
System.out.println(obj.a);
^
Myclass.java:9: <identifier> expected
System.out.println(obj.a);
^
2 errors
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
why there is no reply
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

sekhar kiran wrote:
Myclass.java:9: <identifier> expected
System.out.println(obj.a);
^
Myclass.java:9: <identifier> expected
System.out.println(obj.a);
^
2 errors


What do you think there is wrong with the code above? What do you think the error messages mean?

What's the purpose of the class AX? Why did you define it inside the main() method?
 
Marshal
Posts: 80622
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
And why are the declaration and the print instruction inside the local class?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

sekhar kiran wrote:why there is no reply


Perhaps because nobody has anything to say, or because we're getting a bit frustrated with the fact that, after over 100 posts, this thread doesn't seem to be going anywhere.

Anyway, we're all volunteers here, not your personal answer service; so please EaseUp (←click).

Winston
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jesper de Jong wrote:

sekhar kiran wrote:
Myclass.java:9: <identifier> expected
System.out.println(obj.a);
^
Myclass.java:9: <identifier> expected
System.out.println(obj.a);
^
2 errors


What do you think there is wrong with the code above? What do you think the error messages mean?

What's the purpose of the class AX? Why did you define it inside the main() method?


i think we dont have to declare another class in main method,iam not understanding about error message,declaring class in a method which refere to local class so thats why i tried here
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Winston Gutkowski wrote:

sekhar kiran wrote:why there is no reply


Perhaps because nobody has anything to say, or because we're getting a bit frustrated with the fact that, after over 100 posts, this thread doesn't seem to be going anywhere.

Anyway, we're all volunteers here, not your personal answer service; so please EaseUp (←click).

Winston


i think this is your first post in this thread and even you didnt answer or reply to my post ,then why are you frustrated?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

sekhar kiran wrote:i think this is your first post in this thread and even you didnt answer or reply to my post ,then why are you frustrated?


Because:
(a) Your reply shows just how much notice you take of responses (this is my 4th).
(b) What am I supposed to reply to? This thread has been meandering for the best part of a month; and I don't see any end in sight.

Why don't you simply state exactly what your problem is, and close threads when you get an answer?

Given the subject of this one, I can imagine it being open for as long as you've got "doubt in some program", which is NOT the intent of this site.

Winston
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

sekhar kiran wrote:i think this is your first post in this thread and even you didnt answer or reply to my post ,then why are you frustrated?


Even if someone hasn't posted, that doesn't mean they haven't been following it. I read a lot of threads that I don't post in because someone else has already said what I would say.

It seems like you are just typing various random statements in random places, and when it doesn't work, asking folks here why. And then you got upset when nobody answered you quickly enough. At least, that's how it looks to us.

I would suggest you get a good, basic book, and go through it. Just about everything you have asked would be covered in the first few chapters. Things like default values, and where you can place executable statements or define new classes.

This is not the hard stuff to learn on your own. We generally expect everyone to make SOME effort to learn on their own.

Please try.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

sekhar kiran wrote:i think we dont have to declare another class in main method,iam not understanding about error message,declaring class in a method which refere to local class so thats why i tried here


Arbitrary statements, such as System.out.println(...), must always be in a method or constructor (or inside a static or instance initializer). You cannot put statements at class-level, which is what you were doing in your example. It doesn't matter that class AX is defined inside the main() method; the System.out.println(...) statement is not inside a method or constructor (or an initializer) in the AX class, so it's not allowed there.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Winston Gutkowski wrote:

sekhar kiran wrote:i think this is your first post in this thread and even you didnt answer or reply to my post ,then why are you frustrated?


Because:
(a) Your reply shows just how much notice you take of responses (this is my 4th).
(b) What am I supposed to reply to? This thread has been meandering for the best part of a month; and I don't see any end in sight.

Why don't you simply state exactly what your problem is, and close threads when you get an answer?

Given the subject of this one, I can imagine it being open for as long as you've got "doubt in some program", which is NOT the intent of this site.

Winston


i mean is this mandatory when you get the answer for one doubt then going to close the thread,i already mention title as doubts in program and technical question in java, really its a very helpful forum
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jesper de Jong wrote:

sekhar kiran wrote:i think we dont have to declare another class in main method,iam not understanding about error message,declaring class in a method which refere to local class so thats why i tried here


Arbitrary statements, such as System.out.println(...), must always be in a method or constructor (or inside a static or instance initializer). You cannot put statements at class-level, which is what you were doing in your example. It doesn't matter that class AX is defined inside the main() method; the System.out.println(...) statement is not inside a method or constructor (or an initializer) in the AX class, so it's not allowed there.


i got it,let me confirm is that local class which i defined inside the method?i execute code now

previously i asked about you how the execution occurs,here i post the order which upto my knowledge if anything wrong correct me
class,static variable,static block,main method->checking constructor if it exists invoking instance block,constructor and method else execute the main method statements and terminate the program*/
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

sekhar kiran wrote:i got it,let me confirm is that local class which i defined inside the method?i execute code now


The class AX which you defined inside the main() method is a local class.

sekhar kiran wrote:class,static variable,static block,main method->checking constructor if it exists invoking instance block,constructor and method else execute the main method statements and terminate the program*/


I don't understand what you're trying to say here.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jesper de Jong wrote:

sekhar kiran wrote:class,static variable,static block,main method->checking constructor if it exists invoking instance block,constructor and method else execute the main method statements and terminate the program*/


I don't understand what you're trying to say here.


i mean when we run the program ,execution how it goes is that way correct
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
When you run a program, it starts with the main method in the class you list.

From there, it all depends on what is in your main.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

fred rosenberger wrote:When you run a program, it starts with the main method in the class you list.

From there, it all depends on what is in your main.


i mean first class will be loaded after that static variable and static method will be initialized then it goes to main method->checking constructor if it exists invoking instance block,constructor and method else execute the main method statements and terminate the program
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I stand by my statement. You go to main() first, and then it depends. Example:


In this example, the print statement on line 7 executes first, and then the static block runs.

However, if you switch lines 7 and 8:

The static block runs first. But in both cases, you start with main, and depending on what is there, different things run.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

fred rosenberger wrote:I stand by my statement. You go to main() first, and then it depends. Example:


In this example, the print statement on line 7 executes first, and then the static block runs.

However, if you switch lines 7 and 8:

The static block runs first. But in both cases, you start with main, and depending on what is there, different things run.


i think it is instance block in 2nd line,is that static block or instance block
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
oops...sorry...you're talking about this?


I would suggest you try it yourself and see what happens.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

fred rosenberger wrote:oops...sorry...you're talking about this?


I would suggest you try it yourself and see what happens.


here i know it will execute first static block and goes to main method and print the statement,ok i will say one example a program which consists of static variables,instance variables,static block,instance block,constructors,some methods and statements which order it execute sequence
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Strings are immutable objects .. output will be pqr .

Will Zelan wrote:
what is the output of the program

2)sorting array without third variable in below code i didnt understand logic anyone tell me

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

shyam sundar R wrote:Strings are immutable objects .. output will be pqr .



String immutability has nothing to do with it. The reason the output is pqr is because Java passes references by value.

And I believe this was covered about a bazillion posts ago. Please read the thread before posting, to avoid going over old ground.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

sekhar kiran wrote:i mean is this mandatory when you get the answer for one doubt then going to close the thread


Pretty much: Yes.

You plainly haven't read the UseOneThreadPerQuestion (←click) page - or, for that matter, HowToAskQuestionsOnJavaRanch (←click).

Please do so now.

i already mention title as doubts in program and technical question in java


Hey, why stop there? Why not start a "Please tell me everything there is to know about Java" thread? You could keep that open for years.

Actually...don't.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

sekhar kiran wrote:here i know it will execute first static block and goes to main method and print the statement,



Ok, so you understand order of execution then? Good.

ok i will say one example a program which consists of static variables,instance variables,static block,instance block,constructors,some methods and statements which order it execute sequence



If you want such an example, you can easily create it yourself.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Winston Gutkowski wrote:

sekhar kiran wrote:i mean is this mandatory when you get the answer for one doubt then going to close the thread


Pretty much: Yes.

You plainly haven't read the UseOneThreadPerQuestion (←click) page - or, for that matter, HowToAskQuestionsOnJavaRanch (←click).

Please do so now.

i already mention title as doubts in program and technical question in java


Hey, why stop there? Why not start a "Please tell me everything there is to know about Java" thread? You could keep that open for years.

Actually...don't.

Winston


ok i have doubt about some other topic which are not related to execution,can i post here or can i create a new thread
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Other topic == new thread. It's not a difficult concept.
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jeff Verdegan wrote:

sekhar kiran wrote:here i know it will execute first static block and goes to main method and print the statement,



Ok, so you understand order of execution then? Good.

ok i will say one example a program which consists of static variables,instance variables,static block,instance block,constructors,some methods and statements which order it execute sequence



If you want such an example, you can easily create it yourself.


i will post the order which is understand upto my knowledge till now, if anything goes wrong inform me,here posting when the run the program the class wil be loaded after that static variable,static block,main method->checking constructor if it exists invoking instance block,constructor and method else execute the main method statements and terminate the program
 
sekhar kiran
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jeff Verdegan wrote:

sekhar kiran wrote:here i know it will execute first static block and goes to main method and print the statement,



Ok, so you understand order of execution then? Good.

ok i will say one example a program which consists of static variables,instance variables,static block,instance block,constructors,some methods and statements which order it execute sequence



If you want such an example, you can easily create it yourself.


i will post the order which is understand upto my knowledge till now,here posting when the run the program the class wil be loaded after that static variable,static block,main method->checking constructor if it exists invoking instance block,constructor and method else execute the main method statements and terminate the program ,ff anything goes wrong correct me
 
Campbell Ritchie
Marshal
Posts: 80622
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This correspondence now closed.
 
    Bookmark Topic Watch Topic
  • New Topic