• Post Reply Bookmark Topic Watch Topic
  • New Topic

A few basic questions  RSS feed

 
Aditya Desai
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.I have seen variables declared and initialized in main and not marked static and are used in for loop in the main...and it gives no error..!!!
the variables that must be accessed from the main must be marked static..!!!???

2 Differentiate between object creation and object instantiation..?

3 Is it necessary for a getter and setter method to have a body?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) No. Methods called from main() must be static. Variables can be anything.

2) You create an object with the new keyword. You initialize the object by setting it to some value.

3) Yes. All non-abstract methods must have a body.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Desai wrote:1.I have seen variables declared and initialized in main and not marked static and are used in for loop in the main...and it gives no error..!!!
the variables that must be accessed from the main must be marked static..!!!???
In that case you are probably using the main method incorrectly.
2 Differentiate between object creation and object instantiation..?
I think Knute misread your question. There is no difference between object creation and instantiation. Only you usually say, “instantiate a class,” which tells you which class the object is made from.
3 Is it necessary for a getter and setter method to have a body?
Already answered.
 
Aditya Desai
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the answers..But i still havent got what you told me about question 1
I have read everywhere that you can only access a variable in main without object which is marked static...
but i have seen many questions which initializes and uses non static variables in loops in main and gets no error
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Desai wrote:I have read everywhere that you can only access a variable in main without object which is marked static

Can you give a reference where you read this.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
inside main you can have any type of variable
it can be static or non static.you might get error when you try to use instance data members inside main function this will give you error
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya Desai wrote: . . . I have read everywhere that you can only access a variable in main without object which is marked static... . . .
You can only access static members of the surrounding class from the main method without giving an object name. If you have a reference to an object, you can access instance members too. I think you have misunderstood those references.

you should not be writing lots of code in the main method. One statement is the ideal length. You can see an example here. Note that is one statement not two because the first line is a declaration.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!