| Author |
can class be declared static?
|
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
can class be declared static
if no then how come
static Scanner=new Scanner(System.in);
is valid
and wats the System.in declared within the parenthesis is it that -- in is a method declared in System class
|
http://www.lifesbizzare.blogspot.com || OCJP:81%
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
That is not a class you are declaring static, but a reference to a Scanner object. Yes, you can have static references, as you have already seen.
System.in is not a method because it hasn't got enough (). It is a static member of the System class, which is a reference to a Stream which takes input from the keyboard at the command window (unless you change it). The Scanner uses the System.in as a constructor parameter, which tells it where to read from.
You can actually have static classes, but that is something completely different.
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
|
but isnt reference to an object possibble if it has a class there must be a class called Scanner
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3673
|
|
Vishal Hegde wrote:but isnt reference to an object possibble if it has a class there must be a class called Scanner
Not getting that properly. Are you asking there should be a class called Scanner? Then there is one in java.util.Scanner
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
|
ok campbell ,can referenced objects can be declared static,public,private etc
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
|
Vijitha Kumara you are right and i think i made a stupid question as objects cannot be initialised without the instance of the class
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
Vishal Hegde wrote:ok campbell ,can referenced objects can be declared static,public,private etc
Yes. But only if they are used as fields. Local variables mustn't be called private, public, static, etc.
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
|
campbell plese illustrate a small example of how to assign public,private,static etc to the referenced objects and not assigining it 2 local variables
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
Well you the scope of a local variable right.....that's why its local local to the method, code block. So if its scope is so limited then there is no need for them to have access or non access modifiers. Well only one non access modifier that is final can be used for local variables. And about class, yes a class can be marked static only if it is a nested class that is inside a class. Top level classes can never be static.
Well class as you say is just a blue print which the JVM uses to create the object when it encounters a 'new' keyword. And that object should be referenced by a live thread(main thread) or from an object(instance references) so that we can use it. So when references are as instance references then we have a choice of making it 1 for the class(static) or 1 for each instance that is created.
|
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
Vishal Hegde wrote:static Scanner=new Scanner(System.in);
That is an example of declaring and initialising a static field of a class. It ought to have been declared "private static Scanner . . . " and ought to have been given a name
With those corrections:
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
suppose i created a class named Abc
class Abc
{
}
class Xyz
{
public static void main(String args[])
{
private Abc a=new Abc();
public Abc a=new Abc();
static Abc a =new Abc();
}
is it correct
|
 |
sujith Acharya
Ranch Hand
Joined: Dec 25, 2006
Posts: 60
|
|
|
No, since you are declaring instance of class ABC inside the method Main, it will be local variable for that method. As cambell said, local variables can not be public/private/protected/static
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
class Abc
{
}
class Xyz
{
private Abc a=new Abc();
public Abc a=new Abc();
static Abc a =new Abc();
public static void main(String args[])
{
}
if yes then how will be the variables of class Abc initialised as they are out of the main method
|
 |
sujith Acharya
Ranch Hand
Joined: Dec 25, 2006
Posts: 60
|
|
The code will get complier error, as same variable name is used for more than one member , public ABC a, static ABC a ..etc
If it was different,
The static field will get initiailized when the class XYZ is loaded , and other 2 are initiailized when you create the object of XYZ.
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
|
in this case will the assigning of references to the class variables be done during compile time???
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
Well its the instance variables of class xyz of the type ABC. its similar like Integer(wrapper) i. So i is a wrapper of the type Integer. Same goes to String. String is a final class. So if class xyz has String s then its a instance variable of class xyz of type String. Well instance variables can have access modifiers like private public or protected. In a method, we have local variables i.e local to the method.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
Vishal Hegde wrote: . . . how will be the variables of class Abc initialised as they are out of the main method
They will be initialised by the JVM when an instance of the Xyz class is created.
|
 |
Fred Hamilton
Ranch Hand
Joined: May 13, 2009
Posts: 679
|
|
|
I can't imagine any need to actually declare a class as static. If you want to treat a class as if it were static, then fill it with static methods and variables, and don't instantiate it.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12953
|
|
Fred Hamilton wrote:I can't imagine any need to actually declare a class as static.
You can declare a class to be static, but it might mean something different than you think it does.
Top-level classes cannot be declared static, but nested classes can. See Nested Classes in Sun's Java Tutorial. The tutorial explains what the difference is between non-static and static nested classes.
In practice, you often want to make a nested class static.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Fred Hamilton
Ranch Hand
Joined: May 13, 2009
Posts: 679
|
|
Jesper Young wrote:
Fred Hamilton wrote:I can't imagine any need to actually declare a class as static.
You can declare a class to be static, but it might mean something different than you think it does.
Top-level classes cannot be declared static, but nested classes can. See Nested Classes in Sun's Java Tutorial. The tutorial explains what the difference is between non-static and static nested classes.
In practice, you often want to make a nested class static.
Noted thanks, learn something every day. Indeed, the meaning described by the tutorial was quite different to that which I am familiar with.
|
 |
 |
|
|
subject: can class be declared static?
|
|
|