• 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

Constructor

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following questions. I need help in explaining why the behavior of Java is like this?
1) When a Static variable is incremented within a construtor defined with access public , then the constructor is not called at all?. Why?
2) Why do we have problems when we dont describe main as static? Is that posible to run a main without making them as static. We have a run time error for a non static main?
3) Is that not possible to define ( not initiate ) a variable in a Contructor? Why ?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. It should work. Try this example
class Test1
{
public static int m_x =2 ;
Test1()
{
m_x++;
}
}

class Test2
{
public static void main(String[] args)
{
Test1 t = new Test1();
System.out.println(t.m_x);
}
}
2.The method main() must always be declared as static. The reason is, before an application starts execution, no objects exist, so in order to start execution, you need a method that is executable even though there are no objects. You can execute a static method even when no objects of a class exist, whereas instance methods (not static) can only be excuted in relation to a particular object
3.You can declare a variable within a constructor, but the scope of that variable will be local.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raju,
Check this link for some more info on constructors.
Jeban
 
Raju Anand
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jayashree and Jonathan
Thank you very much for the anwsers and help. Honestly speaking I did not expect such sincere and helping hands. Thanks again and expecting the same in future as well. ( too much eh?)
But Jayashree, for the first question, what I meant by saying access public is not for variable, but for the constructor itself!!. It did not work for me when i tried defining the constructor as public.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) When a Static variable is incremented within a construtor defined with access public , then the constructor is not called at all?. Why?

Hi Raju, Here is answer of your Q1 & Q3

This code is working fine. you can declare a vaiable in a constructor but that will be as a local variable with access type will be default(no public , no static)
class ConstructorTest1
{
public static int m_x =2 ;
public ConstructorTest1()
{
int i =10;
++m_x;
System.out.println("Variable defined in Constructor :" + i);
}
}

class ConstructorTest
{
public static void main(String[] args)
{

ConstructorTest1 t = new ConstructorTest1();
System.out.println(t.m_x);
}
}
2) Why do we have problems when we dont describe main as static? Is that posible to run a main without making them as static. We have a runtime error for a non static main?
Ans: The Problem is when you call an application without declaring main as public staic because the JRE does not know what to do, where to start, how to go ahead.
When you are declaring method as a static its allocating a memory in the memory area, which gets address in the form of binary,another thing is because of the main being static, it could be called directly on behalf of that particular address, before doing anything else, creating any object or any thing.
This is public, so that it can be accesses globally.
I would like to request javaGuru's that If I'm wrong please correct me.

TWR
JAYDEEP
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is only for question 2.
In side the class, the method main() is just a method, same as if you decided to have a method named doStuff(). As far as the compiler is concerned, you are free to give whatever modifiers and return types to the method, as you please.
However, outside of the class the convention is that a static method named main() will be used when the class is called from a command line. Having a main() method declared differently to the convention is liable to confuse programmers, even if the class is never going to be run via a public static void main(String[]).
 
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic