• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

can class be declared static?

 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but isnt reference to an object possibble if it has a class there must be a class called Scanner
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok campbell ,can referenced objects can be declared static,public,private etc
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in this case will the assigning of references to the class variables be done during compile time???
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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:
  • Quote
  • Report post to moderator

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.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
I'm so happy! And I wish to make this tiny ad happy too:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic