Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Declaring an object with the static modifier

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Location
    SPAIN- BCN
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Declaring an object with the static modifier

    Hello!
    I am quite new to programming in Java so I have difficulty trying to grasp why things work the way they do and such. This question is about the use of static for the declaration of an object. I attach the code below:
    public class Main {
     
        private static Scanner scanner = new Scanner(System.in);
     
        public static void main(String[] args) {
            System.out.println("Type in:");
            int count = scanner.nextInt();
            scanner.nextLine();
    }
    Can someone explain to me why it is done that way and the advantages? Thanks a lot.

    Regards.
    Hector_M
    Last edited by Hector_M; December 17th, 2019 at 04:58 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Declaring an object with the static modifier

    the static modifier means a variable belongs to the class and can be referenced and used without an instance of the class.
    Code inside of a static method can only access variables outside of the method that are static.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Hector_M (December 17th, 2019)

Similar Threads

  1. Replies: 2
    Last Post: April 8th, 2014, 12:05 PM
  2. [SOLVED] Error setting up Object. Issue with static vs non-static
    By DaedricSHeep in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 3rd, 2014, 12:11 PM
  3. Need help with declaring static method for code.
    By Marcusjamaal in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 10th, 2012, 12:06 AM
  4. Declaring static character array
    By jts0541 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 1st, 2012, 09:44 AM
  5. Error writing to an array with the static modifier.
    By handuel in forum Collections and Generics
    Replies: 2
    Last Post: December 25th, 2011, 11:10 AM