Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6169135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:48:18+00:00 2026-05-23T22:48:18+00:00

I am writing a simple Interface class as below. I need to make only

  • 0

I am writing a simple Interface class as below. I need to make only one of its methods available to the user. The user using this class needs only the return value of this particular method (checkLink). Therefore I have declared this method alone as protected so it is visible only to the user calling the Iface class. Since the rest of the methods in the Class are to be used within the class they have been declared as private. I added Static keyword as these methods are sort of “stateless”. I used static final keyword for the class variables as I wanted something like a C like #define.

Since I am C programmer I am still new to the Java world. Do take a look at the skeleton of my code and tell me if the declarations made are right in conveying the meaning.

package com.prog.Test;
import java.net.*;
import java.io.*;

public class Iface{   

    private static final int MSG_OK     =   0x01;
    private static final int MSG_NOK    =   0x00;   
    private static final int MSG_FAIL   =   0xFF;    

    private static byte[] getBytesFromUrl(URL link) throws IOException {
        ......                  
        return bytes;
    }               

    private static int[] msg_header(byte[] msg) throws Exception
    {
        int len = msg.length;
        System.out.println("len ="+len);
        int[] headerMsg =new int [3]; 
        headerMsg[0]= MSG_OK; 
            ......

        return headerMsg;
    }

    private static byte[] Tobyte(int[]src) {
            ....
    }

    protected static int checkLink (URL url ) throws IOException {

       byte[] rbuffer = null;
       byte[] hdr = null;
       int status = -1; 

        try{             
            rbuffer = getBytesFromUrl(url);
            ..
            ..
            hdr = Tobyte(msg_header(rbuffer));
            ...
            ...
            status = 0; 

        }catch (Exception e) {
            System.err.println("Exception:  " + e);
        }
        return status; 

  }
}

So when I use this Iface class in an Application, I call it this way:

public class Trial {  

    public static void main (String [] args ) throws IOException {

        URL url = new URL("http://localhost:8000/myfile");

        System.out.println("Calling Iface");

        int retval = Iface.checkLink(url);      

        System.out.println("retval ="+retval);
    }
}

I also tried this (after removing the static keyword in checkLink() method):

 public class Trial {  

        public static void main (String [] args ) throws IOException {

            URL url = new URL("http://localhost:8000/myfile");

            System.out.println("Calling Iface");

                Iface callInterface;

            int retval = callInterface.checkLink(url);      

            System.out.println("retval ="+retval);
        }
    }

Is there a difference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:48:19+00:00Added an answer on May 23, 2026 at 10:48 pm

    Technically, neither of the code examples you posted should work.

    The protected keyword tells Java to only allow classes to access that variable if they extend the Iface class. You are not extending the class in your calling code, so neither approach to using the method should compile if the method is not public.

    Be aware that making the method static means that callers will need to use the Iface class directly, making it impossible to use class inheritance. So if you will ever want to have another class that extends Iface and overrides the implementation of checkLinks, you’d want to make the method non-static.

    If the method is non-static, you’d want to use it like this:

    Iface callInterface = new Iface();
    int retval = callInterface.checkLink(url);   
    

    You may also want to do some research on the factory pattern and the dependency injection pattern, which give you more flexibility in determining which specific Iface implementation you want to use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a pythonic web API wrapper with a class like this import httplib2
I'm writing a doubly-linked list using TDD approach. This collection type is not thread
I figured this should be easy, but I am having problems writing a simple
I'm writing a simple gadget to use with google calendar. I'm using iGoogle to
I'm writing a simple plugin based program. I have an interface IPlugin which has
How long does it take for an experienced Windows programmer to learn writing simple
I'm writing a simple OpenGL application that uses GLUT . I don't want to
I'm writing a simple app that's going to have a tiny form sitting in
I am currently writing a simple, timer-based mini app in C# that performs an
I am writing a simple checkers game in Java. When I mouse over the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.